After a long day, and another... after reading lots of wikis and faqs, it finally works.
What I did:
- Use Jackson
- Force Jackson providers usage
- Define custom deserializer
- activate the ACCEPT_SINGLE_VALUE_AS_ARRAY flag
- 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:
Customizing-ObjectMapper
After doing all of this it still didn't work...
After some more search time, I found:
Jackson 2.0 with Jersey 1.12
That discusses dependencies problems..
This reveled my problem, Jersey v1.13 ships with Jackson v1.9.2 I need Jackson v2.0
I removed dependency for jersey-json, because it included jackson 1.9.2:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
</dependency>
And directly declared dependency for:
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
</dependency>
Reference:
jackson-jaxrs-json-provider
Note: this change removes the Jersey ability to use Jaxb or jettison.
Off topic, may be interesting for someone:
Configure Jersey/Jackson to NOT use @XmlElement field annotation for JSON field naming