Alex Rivera | Logout

Jersey: Can not deserialize instance of ArrayList out of String

Asked 2013-01-14T13:44:19.013
13

I have a json based REST Web Service implemented using: Jetty, Jersey, Jersey-JSON using Jackson.

One of my methods receives a Person instance, which has a field of type List<String>. i.e.:

Public class Person {
    List<String> names;
}

If I call it with an array of names, all works ok! e.g.:

{ "names" : [ "Jhon", "Doe" ] }

But if the person has only one name, my client creates a single value element, e.g.:

{ "names" : "Jhon" } 

When I try to call the service with a single value, I get an exception:

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

Question:

How should I create/configure my web service, in order to be able to deserialize array field when they are sent to me as a single element.

--

I already read:

Jackson deserialization - with contained ArrayList<T>

and

How can I customize serialization of a list of JAXB objects to JSON?

and this that refer the last answer:

Jersey client can not deserializable json services - exception(can not deserialize instance)

Jaxb json missing brackets for one element array

But none of those fix the problem.

Thank you in advance!

Edit
Report

1 Answer

14

After a long day, and another... after reading lots of wikis and faqs, it finally works.

What I did:

  1. Use Jackson
  2. Force Jackson providers usage
  3. Define custom deserializer
  4. activate the ACCEPT_SINGLE_VALUE_AS_ARRAY flag
  5. Fix dependencies

The story:

I was using Jersey 1.13 that uses (I belive) jaxb by default.

I changed it to use Jackson

<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>

as described at:

https://stackoverflow.com/a/13895768/660990

This made my jersey use jackson, but the problem remains; jackson can't deserializer the array yet.

I forced the usage of Jackson providers:

<init-param>
    <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>
        your.project.packages;
        org.codehaus.jackson.jaxrs</param-value>
</init-param>

https://stackoverflow.com/a/3143214/660990

Was a needed step, but not enough. It's necessary to activate the ACCEPT_SINGLE_VALUE_AS_ARRAY flag.

ObjectMapper objectMapper;
objectMapper = new ObjectMapper().configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

I needed define custom deserializer

public class MyResolver implements ContextResolver<ObjectMapper> {

@Override
    public ObjectMapper getContext(final Class<?> objectType) {
        ...            
        return objectMapper;
    }
}

in order to do that:

answered 2013-01-18T16:07:11.253

Your Answer