2

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?

Svilen
  • 1,377
  • 1
  • 16
  • 23
  • Hi, i am recently facing this issue. Have you found any workaround? – Rajkishan Swami Jun 29 '16 at 04:27
  • Unfortunately no. The only way to keep using the jersey 1 custom json representation is to reuse the json providers coming with jersey 1 in jersey 2. – Svilen Jun 30 '16 at 05:58
  • can you please tell me which jars in particular i should add in jersy 2 so that the root tag should be ignored? Currently the jettison dependency is `jersey-media-json-jettison` version `2.23.1`. – Rajkishan Swami Jun 30 '16 at 06:14
  • I guess I wasn't clear... I didn't find a way to unwrap the root element with ANY of the providers - jettison, jackson or moxy. I had to get the jersey 1 code and MANUALLY create my own jersey2 json providers in order to keep the jersey 1 json representation. – Svilen Jun 30 '16 at 12:10

0 Answers0