Dmytro Chyzhykov's Blog

Yet another programmer.

Upgrade Jersey 1.x to Use Jackson 2.x

The first new major update for Jackson since the official 1.0 release was released March 25, 2012.
But the current version (1.17.1) of Jersey 1.x still uses Jackson 1.9

If your project has REST integration tests and you sure that update is really needed and possible (there is no library in your class pass which depends strongly on Jackson 1.x), you can follow this simple and short manual to upgrade.

Remove

The com.sun.jersey.config.property.packages <init-param> from com.sun.jersey.spi.container.servlet.ServletContainer servlet definition in web.xml

1
2
3
4
<init-param>
    <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>org.foobar.rest.services</param-value>
</init-param>

jersey-json artifact

1
2
3
4
5
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.17.1</version>
</dependency>

and Jackson 1.x artifact from dependencies in pom.xml

1
2
3
4
5
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-jaxrs</artifactId>
    <version>1.9.13</version>
</dependency>

Add

for JSON

1
2
3
4
5
<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-json-provider</artifactId>
    <version>2.2.3</version>
</dependency>

for XML

1
2
3
4
5
<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-xml-provider</artifactId>
    <version>2.2.3</version>
</dependency>

So, in the simplest case your Jersey Servlet definition in web.xml would look something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<servlet>
    <servlet-name>Jersey Dispatcher Servlet</servlet-name>
    <servlet-class>
        com.sun.jersey.spi.container.servlet.ServletContainer
    </servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>
            <!--
              Your application packages go here
            -->

            <!--
              Jackson 2.x JAX-RS @Providers
            -->
            com.fasterxml.jackson.jaxrs.json  <!-- JSON -->
            com.fasterxml.jackson.jaxrs.xml   <!-- XML -->
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

Read also: