1

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?

2
  • 1
    When you call what method? With what data? Commented Aug 8, 2022 at 22:06
  • I call in a resource class. It's a PUT. The data come the front end angular. Commented Aug 9, 2022 at 0:52

2 Answers 2

1

The start of the object should be like this { ,Make sure the json from the Front doesn't start with [

{
  "data": "any value"
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks to answer. I checked and the object started with { ... }. I think the problem is in the backend. Do you have any idea?
The object DateTime in the frontend: export interface dateTime { year: number; monthValue: number; month: string; dayOfMonth: number; dayOfYear: number; dayOfWeek: string; hour: number; minute: number; second: number; nano: number; chronology: Chronology; }
@LucianaOliveira There is no date: string; Are you expecting Jackson to convert all those separate date-related fields into a date, and then convert the date to a string, and then store it into the date field? That's not how it works. The model on both the frontend and the backend need to match.
@LucianaOliveira It doesn't matter if the application is new or old. That isn't how Jackson works. You have to make the data models match. If you cannot change the frontend or the backend then it will simply never work.
@LucianaOliveira Do the answers to this question help you? For the one that suggests taking the first 10 characters, I think you could use the entire ISO string if it was a ZonedDateTime on the backend.
|
0

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();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.