Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

json - 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...