12

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

lstachowiak
  • 344
  • 1
  • 2
  • 7

2 Answers2

17

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.

OndroMih
  • 7,280
  • 1
  • 26
  • 44
Michal Gajdos
  • 10,339
  • 2
  • 43
  • 42
  • Where can I find Jackson class location? – Dejell Jan 29 '14 at 21:55
  • 1
    `JacksonFeature` is in `org.glassfish.jersey.media:jersey-media-json-jakson`. This module brings Jackson 1.9.x with it. If you want to use Jackson 2.x simply add dependency on `com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider` and it will be automatically enabled thanks to `META-INF/services` mechanism. – Michal Gajdos Jan 29 '14 at 22:39
  • See [this answer](http://stackoverflow.com/questions/23730062/use-iso-8601-dates-in-jax-rs-responses) for complete instructions to achieve this on GlassFish 4 with a Maven build. – Steve May 20 '14 at 00:47
4

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>
okcomputer_kid
  • 491
  • 6
  • 12
  • 1
    I also add it to pom.xml and start glassfish 4.1. it is still keep jackson version 2.3.2 (default of glassfish) – Tien Nguyen Sep 20 '18 at 03:28