一尘不染

如何轻松使用Jackson的球衣

json

我想使用Jersey来实现一些Java
REST服务。默认的POJO转换器JAXB处理JSON对象有点奇怪,因此我想使用Jackson转换POJO的开销很少或更少。我如何告诉J2ME框架使用另一个转换器?

我的web.xml看起来像这样:

<!-- 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>

阅读 220

收藏
2020-07-27

共1个答案

一尘不染

只需添加jackson JAR并修改web.xml即可,如下所示:

<!-- 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>

所需的Jackson Jackson
JAR(http://jackson.codehaus.org/)是:

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
2020-07-27