• Register
0 votes
288 views

Problem :

I want to POST the List of custom objects. My JSON in a request body is as below :

{
    "collection": [
        {
            "name": "Test ordert1",
            "detail": "ahk ksa"
        },
        {
            "name": "Test ordert2",
            "detail": "Fistekua"
        }
    ]
}

 

But I am facing below error:

“Can not deserialize instance of java.util.arraylist out of start_object token”

How can I fix the above error?

7 5 2
3,870 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

The problem is in the JSON - this cannot, by default, be the deserialized into the Collection because it's not actually the JSON Array - that would look like below:

[
    {
        "name": "Test ordert1",
        "detail": "ahk ksa"
    },
    {
        "name": "Test ordert2",
        "detail": "Fistekua"
    }
]

As you are not controlling a exact process of a deserialization (RestEasy does)

The first option - would be to just inject a JSON as the String and then take the control of a deserialization process as below:

Collection<COrder> readMyValues = new ObjectMapper().readMyValue(
    jsonAsMyString, new TypeReference<Collection<COrder>>() { }
);

You would loose the bit of a convenience of not having to do that by yourself, but you would easily sort out a problem.

Second option - If you cannot change a JSON - would be to construct the wrapper to fit a structure of the JSON input - and use that instead of the Collection<COrder>.

9 7 4
38,600 points

Related questions

0 votes
1 answer 4.6K views
4.6K views
Problem : I am trying to POST a List of custom objects but i am getting exception as below can not deserialize instance of java.util.arraylist out of start_object token
asked Oct 22, 2019 peterlaw 6.9k points
0 votes
1 answer 507 views
507 views
I am getting an issue where my deployable jar hits an exception : can not deserialize instance of java.lang.string out of start_object token
asked Oct 23, 2019 peterlaw 6.9k points
0 votes
1 answer 461 views
461 views
Problem : I am very new to Spring Boot. I am learning to write code in Spring Boot. I have written my first Spring Boot code as below @RequestMapping(value = "/bar/foo", method = RequestMethod.GET) public MyResponseEntity<foo> foo() { Foo model; ... return ... .core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.3</version> </dependency> How can I fix above Spring Boot exception?
asked Feb 29, 2020 mphil 2.3k points
0 votes
1 answer 44 views
0 votes
1 answer 6.1K views
6.1K views
Problem : I am running into the json parsing issue when using a ObjectMapper class from a com.fasterxml.jackson.databind package, and a error that I am getting is: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.graybar.utilities.ups.beans. ... no control over a data I receive. Is there any annotation that needs to be added to be able to remove this issue?
asked Jan 18, 2020 jwilliam 3.9k points
1 vote
2 answers 30 views
30 views asked Sep 10, 2020 rm02 310 points
0 votes
1 answer 79 views
79 views
Problem : I am trying to convert the certain JSON string to the Java object. I am trying to use Jackson for JSON handling. I have totally no control over the input JSON I am reading it from a web service. Below is my input JSON ... 1, column: 23] (through reference chain: MyWrapper["wrapper"]) at org.codehaus.jackson.map.exc.UnrecognizedPropertyException from(UnrecognizedPropertyException.java:63)
asked Dec 16, 2019 alecxe 7.5k points
0 votes
1 answer 2.8K views
2.8K views
Problem : I need to build the Spring-Boot *.war with maven, but I keep facing below error: [WARNING] The requested profile "pom.xml" could not be activated because it does not exist. After completely running the mvn: clean. After the success my ... : Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project webconsultas: Fatal error compiling
asked Dec 24, 2019 alecxe 7.5k points
0 votes
1 answer 25 views
25 views
Help me by solving this poblem.
asked Dec 17, 2020 TeamScript 9.5k points
0 votes
1 answer 1K views
1K views
Problem : I am getting bellow error can only iterate over an array or an instance of java.lang.iterable
asked Nov 1, 2019 peterlaw 6.9k points