Jackson集成(Jersey 2.X)

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 {
    private String code;
    private String message;
    private T data;
}

以上为通用的接口返回对象,若基于Jersey2.X在返回时会出现异常,例如:

//相应对象泛型为基础类型
ResObject res = new ResObject<>();
res.setData(1);

How?

  • 添加maven依赖

    org.glassfish.jersey.media
    jersey-media-json-jackson
    ${jersey.version}

  • 修正Jersey配置 方式一(javax.ws.rs.Application)

    Jersey REST Service
    org.glassfish.jersey.servlet.ServletContainer
    
        javax.ws.rs.Application
        AppsResource
    
    1

-修正Jersey配置 方式二(web.xml配置)


    Jersey REST Service
    org.glassfish.jersey.servlet.ServletContainer
        
            jersey.config.disableAutoDiscovery
            true
        
        
            jersey.config.server.provider.packages
            com.ws.jaxrs.jersey;org.codehaus.jackson.jaxrs
        
    1

注: 需要禁用Jersey的自动扫描选型来禁用Moxy

参考:
Jersey 使用参考
Jersey 配置属性
Jersey Guide
Jersey 集成Jackson

你可能感兴趣的:(Jackson集成(Jersey 2.X))