Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to easily use Jackson for Jersey

I want to use Jersey for implemnting some Java REST services. The default POJO converter JAXB handles JSON objects a bit strange, so I want to use Jackson for converting POJOs with little or less overhead. How would do I tell the J2ME framework to use another converter?

My web.xml looks like this:

<!-- Jersey JAVA REST implementation -->
<servlet>
<servlet-name>Jersey</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>org.foobar.rest.services</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
like image 771
daskai0815 Avatar asked Oct 14 '12 14:10

daskai0815


1 Answers

simply add the jackson JARs and modify the web.xml like this:

<!-- Jersey JAVA REST implementation -->
<servlet>
<servlet-name>Jersey</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>org.foobar.rest.services;org.codehaus.jackson.jaxrs</param-value>    <!--  use jackson for JSON -->
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

The required Jackson JARs ( http://jackson.codehaus.org/ ) are:

jackson-core-asl-1.9.10.jar
jackson-core-lgpl-1.9.10.jar
jackson-jaxrs-1.9.10.jar
jackson-mapper-asl-1.9.10.jar
jackson-mapper-lgpl-1.9.10.jar
jackson-mrbean-1.9.10.jar
jackson-smile-1.9.10.jar
jackson-xc-1.9.10.jar
like image 108
mschmoock Avatar answered Oct 24 '22 09:10

mschmoock