Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force Glassfish4 to use Jackson instead of Moxy

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

like image 725
lstachowiak Avatar asked Aug 19 '13 15:08

lstachowiak


2 Answers

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.

like image 150
Michal Gajdos Avatar answered Nov 20 '22 17:11

Michal Gajdos


Answer by Michal Gajdos is correct, just to add to that, add this dependency in your pom.xml ,

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.26</version>
</dependency>
like image 44
okcomputer_kid Avatar answered Nov 20 '22 18:11

okcomputer_kid