How can I make my mapper object work in a situation when there is another mapper object defined in the dependency jar?
I am trying to use Swagger with Jersey 2 that runs under Jetty. The problem is that as soon as I add the Jag-JX Swagger to the classpath, my mapper object is not detected, so I lose the custom serialization of my objects.
This is how my mapper object was defined
@Provider public class ObjectMapperProvider implements ContextResolver<ObjectMapper> { }
I posted a question for the Swagger companions where you could read the details.
After hours of debugging in Jersey boarding schools, I found that Swagger's own object mapper com.wordnik.swagger.jaxrs.json.JacksonJsonProvider calls super.setMapper(commonMapper) which sets a non-zero value for ProviderBase._mapperConfig._mapper . Later, when the HTTP request handler tries to serialize an instance of my class call, it ends up in ProviderBase.locateMapper which has the following body
public MAPPER locateMapper(Class<?> type, MediaType mediaType) { // First: were we configured with a specific instance? MAPPER m = _mapperConfig.getConfiguredMapper(); if (m == null) { // If not, maybe we can get one configured via context? m = _locateMapperViaProvider(type, mediaType); if (m == null) { // If not, let get the fallback default instance m = _mapperConfig.getDefaultMapper(); } } return m; }
in the correct _mapperConfig.getConfiguredMapper() returns null, which subsequently calls the _locateMapperViaProvider call that my user matching finds. With Swagger, com.fasterxml.jackson.jaxrs.json.JsonMapperConfigurator used by com.fasterxml.jackson.jaxrs.json.JsonMapperConfigurator and my custom json serializers are never called.
I created a small project that reproduces this problem here .
How do you guys suggest fixing this? I could point a deserializer to every property of type TTLocalDate but it will pollute the code :(
expert Feb 10 '15 at 12:16 2015-02-10 12:16
source share