I've been asked to beautify default Jackson JSON coming out of a RestEasy endpoint. I did some research on Jackson and wrote some standalone code to be able to suppress nulls, customize data formats etc. Now the challenge is injecting this code in RestEasy's JSON serialization.
Judging from the forum posts this is trivial in Spring, however doesn't seem to be the case in RestEasy. I wrote a ContextResolver and configured as resteasy.provider in context params in web.xml (on Tomcat) but that prevents the webapp from loading on Tomcat.
Now I'm trying to extend javax.ws.rs.core.Application and provide a ContextResolver but making no progress. Is this straight forward, has anyone done this? Any help is greatly appreciated.
I had a lot of work to register it in my application even following the tip to use context-param once I'm using spring boot and have not web.xml, here what I did
Custom provider
Registering on application
META-INF/services/javax.ws.rs.ext.Providers
Provider for Jackson
ObjectMapper
should be standard JAX-RS way of doing this (works with Jersey), so it seems like the way to go with RESTeasy as well.I found a nicer way of modifying the Jackson SerializationConfig - you can intercept the ObjectMapper creation by using a JAX-RS ContextResolver.
You will need to register with RESTEasy in one of the following ways:
Reference: RESTEasy docs
Reference: Nicklas Karlsson on the JBoss forums
Please note that this works with RESTEasy 2.3.2 which ships as a module in JBoss 7.1.1.Final, but does not appear to work with RESTEasy 3.0-beta5.
Ok,I figured it out, I was able to do this by writing a custom JacksonJsonProvider based on the Jackson FAQ: JAX-RS.The code is as follows:
If you're using the Jackson2 provider you need to do something slightly different from the previous answer. This example will pretty-print the output by default
and to register it in your web-xml, if you don't have autoregister on, add it to your resteasy.providers context-param
© RESTEasy User Guide.
This is not a global solution, but you can put the annotation on classes too.