问题
In my current setup I use Jersey 2.0 with MOXy as discribed in jersey docs. I rely completely on the “Auto-Discoverable Features”, so I do not use any extra configuration or JAXB annotation.
My task is to deserialize an array of strings on the server side. The client is sending the JSON message:
["foo","bar"]
And on the server side the following method header should deserialize it:
@POST
@Path("/stringArray")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response stringArray(List<String> stringList) {
...
}
Problem: The content of stringList is {null,null}, so the amount of elements is always correct, but the strings are set to null.
The same with a small wrapper class is working. Here the class:
public static class Data {
public List<String> stringList;
}
Changing methode signature to stringArray(Data data) and changing the JSON message to:
{"stringList": ["foo","bar"]}
What is the difference between the two approaches and how can I get the plain string array working?
Update: The described problem is fixed by answer from @Blaise. But the closely related problem of serializing a List of POJOs does still not work. Message:
[org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=class java.util.ArrayList.]
The only solution I found is again using a small wrapper class containing the List...
回答1:
The issue you are seeing is due to a bug in EclipseLink JAXB (MOXy):
- http://bugs.eclipse.org/412336
This bug has been fixed in the EclipseLink 2.5.1 and 2.6.0 streams. You can download a nightly build starting July 5, 2013 from the following link:
- http://www.eclipse.org/eclipselink/downloads/nightly.php
来源:https://stackoverflow.com/questions/17468305/moxy-json-and-jersey-2-0-does-not-deserialize-plain-string-array