What?
Jackson is a Java JSON API which provides several different ways to work with JSON. Jackson is one of the most popular Java JSON APIs out there.
https://github.com/FasterXML/jackson
总结一下:
流行的Java JSON API
Why?
Jersey 默认集成的Moxy 不是太好用,抑或是我自己没搞明白
示例如下:
public class ResObject<T> {
private String code;
private String message;
private T data;
}
以上为通用的接口返回对象,若基于Jersey2.X在返回时会出现异常,例如:
//相应对象泛型为基础类型
ResObject<Integer> res = new ResObject<>();
res.setData(1);
How?
- 添加maven依赖
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
- 修正Jersey配置 方式一(javax.ws.rs.Application)
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>AppsResource</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
-修正Jersey配置 方式二(web.xml配置)
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.disableAutoDiscovery</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.ws.jaxrs.jersey;org.codehaus.jackson.jaxrs</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
注: 需要禁用Jersey的自动扫描选型来禁用Moxy