We are migrating from Jersey 1 to Jersey 2. Up until now we were using ContextResolver configured like this:
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.api.json.JSONConfiguration.MappedBuilder;
@Provider
@Produces("application/json")
public class JSONJAXBContextResolver implements ContextResolver<Class<?>> {
@Override
public JAXBContext getContext(Class<?> objectType) {
MappedBuilder mapped = JSONConfiguration.mapped();
mapped.arrays("Property"); //$NON-NLS-1$
mapped.arrays("option"); //$NON-NLS-1$
JSONConfiguration build = mapped.xml2JsonNs(NamespacesMapper.getNamespacesMap()).build();
return new JSONJAXBContext(build, objectType);
}
}
All is good, the produced json looked like this(root xml element is unwrapped i.e. removed): {"@id":"as213","code":"ERR12","cause":{"validationMessages":{"validationMessage":{"message":"some message","details":"some details","severity":"ERROR"}}}}
However, with jersey 2 there is no more JSONConfiguration.mapped(). Instead we are looking into jettison way of doing the same thing. So we now have:
@Provider
@Produces("application/json")
public class JSONJAXBContextResolver implements ContextResolver<Class<?>> {
@Override
public JAXBContext getContext(Class<?> objectType) {
MappedJettisonBuilder mappedJettison = JettisonConfig.mappedJettison();
mappedJettison.serializeAsArray("Property"); //$NON-NLS-1$
mappedJettison.serializeAsArray("option"); //$NON-NLS-1$
JettisonConfig build = mappedJettison.xml2JsonNs(NamespacesMapper.getNamespacesMap()).build();
return new JettisonJaxbContext(build, objectType);
}
However this produces the following: {"error":{"@id":"as213","code":"ERR12","cause":{"validationMessages":{"validationMessage":{"message":"some message","details":"some details","severity":"ERROR"}}}}}
Notice the "root" element "error". This breaks our JSON representation big time.
I've spent almost 2 days now trying to figure out how to configure Jettison to exclude the xml root element, but to no avail.
I've noticed the following in the JettisonConfig javadoc: https://jersey.java.net/apidocs/2.1/jersey/org/glassfish/jersey/jettison/JettisonConfig.html#DEFAULT
public static final JettisonConfig DEFAULT
The default JettisonConfig uses JettisonConfig.Notation.MAPPED_JETTISON notation with root unwrapping option set to true.
However even using DEFAULT configuration instead of Mapped does not produce the desired json - the root "error" element is still there.
I've even looked in the Jettison source for configuration property controlling this behavior but could not find anything.
Does anyone know how and if it is possible to make Jettison ignore the root XML element?