问题
I'm using JAX-RS + Jersey to consume the web-service request and Jackson to translate JSON data:
@Path("/")
public class JAXRSRestController {
@Path("/jsonRequest")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response submitJsonRequest(SampleObject sampleObject, @Context HttpHeaders headers)
{
Ack ack = new Ack();
ack.setUniqueId(sampleObject.getId());
ack.setType(sampleObject.getName());
return Response.ok().entity(ack).build();
}
}
Here if the request is in the below format, it would not be consumed:
{
"sampleObject": {
"id": "12345",
"name": "somename"
}
}
But if the request is in the below format, it will be consumed:
{
"id": "12345",
"name": "somename"
}
How can I make the controller consume the Json root element as well?
SampleObject class:
import org.codehaus.jackson.map.annotate.JsonRootName;
@XmlRootElement(name = "sampleObject")
@JsonRootName(value = "sampleObject")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SampleObject", propOrder = {
"id",
"name"
})
public class SampleObject
{
protected String id;
protected String name;
public SampleObject(){}
public SampleObject(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
web.xml:
<?xml version="1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<display-name>Wed Application</display-name>
<servlet>
<servlet-name>Jersey RESTFul WebSerivce</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.jaxrs.rest</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey RESTFul WebSerivce</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
回答1:
There are two approaches I can think of. If this is a common occurrence in your application, I would recommend enabling unwrapping on your ObjectMapper
. If this is a one-off situation, a wrapper object is not a bad option.
A. Enable Unwrapping
@JsonRootName
will only apply if unwrapping is enabled on the ObjectMapper
. You can accomplish this with a deserialization feature. Note that this will unwrap all requests:
public CustomObjectMapper() {
super();
enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
}
If you do not already have a custom ObjectMapper
registered then you will need to add a provider to register your custom config with Jersey. This answer explains how do accomplish that.
B. Create a Wrapper
If you do not want to unwrap globally, you can create a simple wrapper object and omit the @JsonRootName
annotation:
public class SampleObjectWrapper {
public SampleObject sampleObject;
}
Then update your resource method signature to accept the wrapper:
public Response submitJsonRequest(SampleObjectWrapper sampleObjectWrapper, @Context HttpHeaders headers)
来源:https://stackoverflow.com/questions/28229017/jackson-not-consuming-the-json-root-element