Adding JAR with ObjectMapper makes my ObjectMapper inaccessible for reading

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 :(

+5
java jersey jax-rs swagger
Feb 10 '15 at 12:16
source share
1 answer

As fehguy noted in the release report, using the latest version of Swagger and using SwaggerSerializers should fix this problem. If previously Swagger JacksonJsonProvider used for all serializations, SwaggerSerializers used only for the Swagger model object.

 public class SwaggerSerializers implements MessageBodyWriter<Swagger> { @Override public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { return Swagger.class.isAssignableFrom(type); } 
+1
Dec 10 '15 at 9:44
source share



All Articles