javajsonjacksonwebsphereejb-3.0

ERROR: Can not deserialize instance of java.lang.String out of START_OBJECT token


I'm using Jackson version 2.7.4 + EJB 3.0 with WebSphere 9.0. The classe has an attribute String date as below:

Class:

@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
public class filter {

    private String date;
    ...
}

When I call the method shows the error below.

Error:

Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token at [Source: org.apache.cxf.transport.http.AbstractHTTPDestination$1@b3f4efe6; line: 1, column: 163] at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:160) at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:194) at org.codehaus.jackson.map.deser.StdDeserializer$StringDeserializer.deserialize(StdDeserializer.java:607) at org.codehaus.jackson.map.deser.StdDeserializer$StringDeserializer.deserialize(StdDeserializer.java:576) at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:149) at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:237) at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:496) at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:350) at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:1961) at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:889) at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:410) at org.apache.cxf.jaxrs.utils.JAXRSUtils$2.run(JAXRSUtils.java:1408) at java.security.AccessController.doPrivileged(AccessController.java:738) at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBodyReader(JAXRSUtils.java:1404) at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody(JAXRSUtils.java:1354) at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameter(JAXRSUtils.java:878) at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameters(JAXRSUtils.java:837) at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:265) at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:95) ... 29 more

Maven dependencies:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.7.4</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.7.4</version>
</dependency>

Does anyone have any idea?


Solution

  • It's a beginner question, so if you have the same error with different types the solution is the same.

    JsonMappingException: Can Not Deserialize Instance Of

    The Problem : This exception is thrown if the wrong type is used while deserializing.

    The Solution: Checking the attribute has the different types.

    In my problem, the solution was to change the type of angular date to a angular's native date to match to backend java type. You can create a main class to test your backend code like below.

    Thanks the developers that comment this post. I could see the rubber duck technique applyed here. Thanks a lot.

    public static void main(String[] args) {
        
        ObjectMapper objectMapper = new ObjectMapper();
        String json = ""; //Use your string json
        
        try {
            Person personDeserialized = objectMapper.readValue(json, Person.class);
        
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }