I am using Jersey+Jackon to make a REST API which works with JSON.
Assume that I have a class as follows:
@XmlRootElement
public class A {
public
The first one is a valid JSON String.
The second is not.
Your requirement seems to be to drop the root element from json string. This can be configured in Jersey as follows.
In Jersey, whether dropping root element is configured by JSONConfiguration.rootUnwrapping()
. More details can be found in JSON support in Jersey and CXF.
Here's a sample code that does this.
@Provider
public class MyJAXBContextResolver implements ContextResolver<JAXBContext> {
private JAXBContext context;
private Class[] types = {StatusInfoBean.class, JobInfoBean.class};
public MyJAXBContextResolver() throws Exception {
this.context = new JSONJAXBContext(
JSONConfiguration.mapped()
.rootUnwrapping(true)
.arrays("jobs")
.nonStrings("pages", "tonerRemaining")
.build(),
types);
}
public JAXBContext getContext(Class<?> objectType) {
return (types[0].equals(objectType)) ? context : null;
}
}
You could try add the following to your web.xml, under the relevant <servlet>
node:
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
The Jersey documentation has more information on this setting.
This feature was introduced in Jersey 1.4 and depends on Jackson. If you're using the version bundled with Glassfish 3.0.1 or below, you will need to follow the upgrade instructions.