Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Jackson as JSON Provider in JAX-RS 2.0

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>
like image 220
Tim Avatar asked Sep 11 '13 13:09

Tim


1 Answers

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.

like image 200
StaxMan Avatar answered Sep 24 '22 05:09

StaxMan