Register Jackson MixIn in JavaEE Application

Based on this setup (using Jackson as a JAXB provider in a JavaEE application): How can I register MixIn Modules ?

In my client application using the JAX-RS client function, it is automatically registered. I saw this SO post , but where do I get the ObjectMapper from? I tried to create in mine ServletContextListener

and register a module there. But of course the mapping instance will be deleted after the method completes contextInitialized

.

0


source to share


1 answer


Use ContextResolver

as shown in this post . With annotation @Provider

should be scanned ContextResolver

from scan (assuming you are using scan, batch scan or class scan)

@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {

    final ObjectMapper mapper = new ObjectMapper();

    public ObjectMapperContextResolver() {
        mapper.registerModule(new MixinModule());
    }

    @Override
    public ObjectMapper getContext(Class<?> type) {
        return defaultMapper;
    }  
}

      



What's going on is that MessageBodyReader/MessageBodyWrite

your provider Jackson JAX-RS will call a method getContext

to getObjectMapper

0


source







All Articles