Force Glassfish4 to use Jackson instead of Moxy

lstachowiak picture lstachowiak · Aug 19, 2013 · Viewed 10.9k times · Source

Glassfish4 is using Moxy to serialize REST responses into JSON. Does anybody know how to configure application to use Jackson instead of Moxy?

Answer

Michal Gajdos picture Michal Gajdos · Aug 19, 2013

You need to register JacksonFeature in your application if you want to use Jackson as your JSON provider (by registering this feature your disable MOXy to be your JSON provider).

You can do it either in Application subclass:

public class MyApplication extends Application {

    public Set<Class<?>> getClasses() {
        final Set<Class<?>> classes = new HashSet<Class<?>>();

        // Add root resources.
        classes.add(HelloWorldResource.class);

        // Add JacksonFeature.
        classes.add(JacksonFeature.class);

        return classes;
    }
}

or in ResourceConfig:

final Application application = new ResourceConfig()
        .packages("org.glassfish.jersey.examples.jackson")
        .register(MyObjectMapperProvider.class)  // No need to register this provider if no special configuration is required.
        // Register JacksonFeature.
        .register(JacksonFeature.class);

See Jackson section in Jersey Users Guide for more information.