16

I want to use Jackson as JSON provider for my JAX-RS 2.0 webservice. For JAX-RS I use Jersey 2.0 in GlassFish 4. With JAX-RS 1.x I can add

<init-param>
  <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
  <param-value>true</param-value>
</init-param>

in my web.xml. How can i do this in Jax-RS 2.0 with Jersey 2.0? I implement a application class like this

public class MyRESTExampleApplication extends ResourceConfig {
    public MyRESTExampleApplication() {
         packages("com.carano.fleet4all.restExample");
         register(JacksonFeature.class);
    }
}

and add these lines to my web.xml.

<init-param>
    <param-name>javax.ws.rs.Application</param-name>
    <param-value>com.example.restExample.MyRESTExampleApplication</param-value>
</init-param>

But I get an exception by the request org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class ...

My pom.xml looks like this

<dependencies>
  <dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.0</version>
    <scope>provided</scope>
  </dependency>
  <dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.0</version>
    <scope>provided</scope>
  </dependency>
</dependencies>
sanastasiadis
  • 1,182
  • 1
  • 15
  • 23
Tim
  • 643
  • 3
  • 12
  • 23
  • Thank you! Thank you! I had tried all sorts of classpath shenanigans to try and stop this error but registering the feature explicitly was the only way I could fix it – natke May 02 '17 at 03:10

2 Answers2

22

You should only need to get the implementation jar Jackson JAX-RS provider, add that to your classpath, and it should work. Version 2.x uses SPI-based auto-registration, so that you do not need anything in web.xml.

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • This is generally working for me, but Jackson no doesn't use my [MinIns-Annotations](http://wiki.fasterxml.com/JacksonMixInAnnotations) anymore. Do I need to register them manually now? (does it matter that I'm using glassfish 4.1?) – thomas.mc.work Mar 20 '15 at 15:53
  • Mix-in annotations are always explicitly registered. Maybe you are confusing mix-ins with regular annotations? With regular annotations make sure to use the right set: 1.x annotations are NOT the same as 2.x annotations -- names are same, but they live in different Java package, similar to how parser implementations are in different Java package (and have different Maven group id). – StaxMan Mar 20 '15 at 19:05
13

The above code worked for me. JAX-RS 2.0 has auto discovery feature so it should detect your jersey-media-json-jackson.jar. I am using a Tomcat setup so, I had to explicitly call register(JacksonFeature.class) in my application as you do.

Oliver
  • 3,815
  • 8
  • 35
  • 63
SuperNova1054
  • 319
  • 3
  • 8
  • I am getting java.lang.NoClassDefFoundError: org/codehaus/jackson/jaxrs/JacksonJaxbJsonProvider. Which version of the jersey-media-json-jackson jar do you have? what is the full path of JacksonFeature? – Dejell Feb 11 '14 at 19:10
  • It wasn't necessary for me to explicitly register the class with Tomcat 7. I just added this to the pom.xml: com.fasterxml.jackson.jaxrs jackson-jaxrs-json-provider 2.3.0 – Desty Feb 13 '14 at 16:24
  • I am experiencing the same problem. I use Jersey 2.0, Jackson 2.0 and I am not using Maven. In the Tomcat 7 lib folder I have jersey-media-json-jackson-2.3.jar and jackson-jaxrs-json-provider-2.2.3.jar. I tried to register JacksonFeature with the same NoClassDefFoundError: org/codehaus/jackson/jaxrs/JacksonJaxbJsonProvider. If I do not do this, I get: SEVERE: MessageBodyWriter not found for media type=application/json, type=class _myClass_, genericType=class _myClass_. My web.xml is essentially the same as the original poster's one. Advise, please. – Yuri P. Aug 06 '14 at 20:03