Question

I have a jersey webservice running 1.17 and supports returning responses via both XML and JSON via the @Produces annotation. I am assuming it uses JAXB by default when returning JSON responses but I have no way to confirm it. As of now, my existing clients also use the same JAXB serializer/deserializer. I want to create a new client that uses Jackson without impacting the existing clients.

The JAXB JSON response is incompatible for Jackson for Maps. the JSON for a map using JAXB is of the form

"mapName":{"entry":[{"key":"key1","value":"value1"},{"key":"key2","value":"value2"}]}

and Jackson fails to parse this. Is there any way to make jackson parse this JSON?

Another Attempt: Switching Jersey to use Jackson

This isn't the preferred option but I tried setting "com.sun.jersey.api.json.POJOMappingFeature" to true to allow it to use Jackson for JSON Serialization/Deserialization however the service ends up returning 500s on response without logging any exceptions. the log4j logger level is set to TRACE. I enabled the ContainerRepsonseFilter to confirm 500s in the response and to my surprise, it logs the successful 2xx response. My guess is the problem occurs somewhere further down the stack but I don't know where.

Was it helpful?

Solution

I ended up with using MOXy which is able to parse the above json format.

@Provider

public class JsonMoxyConfigurationContextResolver implements ContextResolver {

private final MoxyJsonConfig config;

public JsonMoxyConfigurationContextResolver() {
    final Map<String, String> namespacePrefixMapper = new HashMap<String, String>();
    namespacePrefixMapper.put("http://www.w3.org/2001/XMLSchema-instance", "xsi");

    config = new MoxyJsonConfig()
        .setNamespacePrefixMapper(namespacePrefixMapper)
        .setNamespaceSeparator(':');
}

@Override
public MoxyJsonConfig getContext(Class<?> objectType) {
    return config;
}

}

and enabled it Jersey 2.x client using

cc.register(JsonMoxyConfigurationContextResolver.class);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top