We are using IBM(s) bundled Apache Wink to offer JAXRS endpoints for our application. We are coding towards Websphere 8.5.5. Since we are servlet 3.0 compliant we use the 'programmatic' way of configuring the JaxRS application, meaning no entries in web.xml and we rely on class scanning for annotated jax rs resources. In general it works fine.
@ApplicationPath("/api/v1/")
public class MyApplication extends Application{
This version of Websphere along with Apache Wink, uses Jackson 1.6.x for JSON de/serialization and in general it works well. We would like though to change some of the default values of the Object Mapper
So we have defined a customer context resolver, where just alter some of the se/deserialzation properties.
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class CustomJackssonConverter implements ContextResolver<ObjectMapper> {
final ObjectMapper defaultObjectMapper;
public AibasJackssonConverter() {
defaultObjectMapper = createDefaultMapper();
}
...
mapper.getSerializationConfig().set(SerializationConfig.Feature.INDENT_OUTPUT, true);
During JAX-RS calls we can see that the container registers the new Provider, with no errors
The problem is that , the Configuration is not 'followed', from the logs I can see that the Wink Engine is looking up a WinkJacksonProvider, which in turn..returns a JacksonProvider that is following the Jackson(s) default values?
Is there a way to just change this default value?
I have tried to change the implementation of the Application object as indicated here, in order to configure Providers programmatically, but it did not work.
http://www.ibm.com/developerworks/java/library/wa-aj-jackson/index.html
Any hints or tips?
Many thanks
I solved this problem by just implementing a MessageBodyWriter class, like this:
Every time a JSON serialization is requested, this class comes into action and finally its writeTo method is invoked.
Here SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS is turned off, as requested by WebSphere.
I usse MOXy instead of Jackson in WAS v8.0.0.x.
To override Jackson, I implement my Application class as so:
However, I've noticed that WAS seems to ignore following annotation:
So, I've resorted to using web.xml:
The point is, since WAS or Wink seems to ignore the Application implementation when using the ApplicationPath annotation, Wink loads the default Application class, which uses Jackson by default.
And yes, I've read documentation and even watched IBM videos online that mention that @ApplicationPath allows you to avoid XML config, however this problem seems to be a bug.
UPDATE:
An alternative approach could be what David Blevins has mentioned in another SO post.
Check out the section Using JAX-RS
I found working solution with ContextResource.
You need Jackson JAX-RS provider dependencies. Maven example:
Next you can implement ContextResolver
And finaly you must register JacksonJaxbJsonProvider and your ContextResolver in your Application class.