Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

isah

I'm trying to POST a List of custom objects. My JSON in request body is this:

{
    "collection": [
        {
            "name": "Test order1",
            "detail": "ahk ks"
        },
        {
            "name": "Test order2",
            "detail": "Fisteku"
        }
    ]
}

Server side code that handles the request:

import java.util.Collection;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;


@Path(value = "/rest/corder")
public class COrderRestService {

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response postOrder(Collection<COrder> orders) {
        StringBuilder stringBuilder = new StringBuilder();
        for (COrder c : orders) {
            stringBuilder.append(c.toString());
        }
        System.out.println(stringBuilder);
        return Response.ok(stringBuilder, MediaType.APPLICATION_JSON).build();
    }
}

Entity COrder:

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class COrder {
    String name;
    String detail;

    @Override
    public String toString() {
        return "COrder [name=" + name + ", detail=" + detail
                + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
                + ", toString()=" + super.toString() + "]";
    }
}

But an exception is thrown:

SEVERE: Failed executing POST /rest/corder
org.jboss.resteasy.spi.ReaderException: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
 at [Source: [email protected]; line: 1, column: 1]
    at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:183)
    at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:88)
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:111)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:280)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:234)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:221)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179)
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
Eugen

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

[
    {
        "name": "Test order1",
        "detail": "ahk ks"
    },
    {
        "name": "Test order2",
        "detail": "Fisteku"
    }
]

Since you're not controlling the exact process of deserialization (RestEasy does) - a first option would be to simply inject the JSON as a String and then take control of the deserialization process:

Collection<COrder> readValues = new ObjectMapper().readValue(
    jsonAsString, new TypeReference<Collection<COrder>>() { }
);

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

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

Hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of object out of START_ARRAY token - JAVA

From Java

Jackson error: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token

From Dev

Json Mapping Exception can not deserialize instance out of START_ARRAY token

From Dev

Java can't deserialize JSON

From Dev

How can I add JsonTypeInfo (annotation) on java.util.ArrayList

From Dev

Jackson JsonMappingException: Can not deserialize instance

From Dev

Could not read JSON: Can not deserialize instance of hello.Country[] out of START_OBJECT token

From Dev

Cannot deserialize instance of object out of START_ARRAY token in Spring Webservice

From Dev

Exception: "org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token"

From Dev

Can not deserialize instance of JsonGen out of START_ARRAY token : newbee

From Dev

How can I deserialize a child instance as a parent object without losing its specific property?

From Dev

Can not deserialize instance of java.util.List out of VALUE_STRING

From Dev

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of org.springframework.data.domain.Sort out of START_ARRAY token

From Dev

JSON deserialization throwing exception - Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

From Dev

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize out of START_ARRAY token

From Dev

Can not deserialize instance of Object out of START_ARRAY token

From Dev

Gson deserialize Java.util.Date with timezone

From Dev

Ignore the "Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token" error

From Dev

Can not deserialize instance of my.package.name.PlaceData[] out of START_OBJECT token

From Dev

Can not deserialize instance of java.util.HashMap out of START_ARRAY token

From Dev

AWS Can not deserialize instance of java.lang.String out of START_OBJECT

From Dev

JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token

From Dev

Java REST API: Can not deserialize instance of Object out of START_ARRAY token

From Dev

Exception: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at

From Dev

com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of object out of START_ARRAY token

From Dev

JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token

From Dev

Convert JSON to Object throws JsonMappingException "Can not deserialize instance of class out of START_ARRAY token"

From Dev

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of Object out of START_ARRAY token

From Dev

Why Jackson ObjectMapper give me this error converting JSON field into a Date object? Can not deserialize value of type java.util.Date from String

Related Related

  1. 1

    com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of object out of START_ARRAY token - JAVA

  2. 2

    Jackson error: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token

  3. 3

    Json Mapping Exception can not deserialize instance out of START_ARRAY token

  4. 4

    Java can't deserialize JSON

  5. 5

    How can I add JsonTypeInfo (annotation) on java.util.ArrayList

  6. 6

    Jackson JsonMappingException: Can not deserialize instance

  7. 7

    Could not read JSON: Can not deserialize instance of hello.Country[] out of START_OBJECT token

  8. 8

    Cannot deserialize instance of object out of START_ARRAY token in Spring Webservice

  9. 9

    Exception: "org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token"

  10. 10

    Can not deserialize instance of JsonGen out of START_ARRAY token : newbee

  11. 11

    How can I deserialize a child instance as a parent object without losing its specific property?

  12. 12

    Can not deserialize instance of java.util.List out of VALUE_STRING

  13. 13

    com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of org.springframework.data.domain.Sort out of START_ARRAY token

  14. 14

    JSON deserialization throwing exception - Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

  15. 15

    com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize out of START_ARRAY token

  16. 16

    Can not deserialize instance of Object out of START_ARRAY token

  17. 17

    Gson deserialize Java.util.Date with timezone

  18. 18

    Ignore the "Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token" error

  19. 19

    Can not deserialize instance of my.package.name.PlaceData[] out of START_OBJECT token

  20. 20

    Can not deserialize instance of java.util.HashMap out of START_ARRAY token

  21. 21

    AWS Can not deserialize instance of java.lang.String out of START_OBJECT

  22. 22

    JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token

  23. 23

    Java REST API: Can not deserialize instance of Object out of START_ARRAY token

  24. 24

    Exception: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at

  25. 25

    com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of object out of START_ARRAY token

  26. 26

    JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token

  27. 27

    Convert JSON to Object throws JsonMappingException "Can not deserialize instance of class out of START_ARRAY token"

  28. 28

    com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of Object out of START_ARRAY token

  29. 29

    Why Jackson ObjectMapper give me this error converting JSON field into a Date object? Can not deserialize value of type java.util.Date from String

HotTag

Archive