Jersey&Jackson返回json对象时如何隐藏null字段
举例来说,如下的rest service发布:
package com.xxx.xxx;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/")
public class MyService {
@GET
@Path("/version")
@Produces(MediaType.APPLICATION_JSON)
public ResponseVersion getVersion() {
return new ResponseVersion("v1.0", null);
}
}
返回对象class ResponseVersion的定义如下:
package com.xxx.xxx;
public class ResponseVersion {
private String version = null;
private String description = null;
public ResponseVersion() {};
public ResponseVersion(String version, String description) {
this.version = version;
this.description = description;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
客户端调用请求返回:
$ curl http://localhost:8080/version
{"version":"v1.0","description":null}
我们看到description字段是null值,这很烦人,能不能把它消掉,就是不显示值为null的字段。
方法1:针对特定类和特定域
网上找了半天办法,有人说使用@JsonInclude(JsonInclude.Include.NON_NULL)
,如下:
package com.oracle.bcs.gateway;
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseVersion {
private String version = null;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String description = null;
public ResponseVersion() {};
public ResponseVersion(String version, String description) {
this.version = version;
this.description = description;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
放在类的定义上对整个类有效,放在字段上针对特定字段有效。
方法2:针对全局
另一个办法我测试过有效,但是他改的是全局属性,不是针对特定类,或者特定字段。
第一步,自定义你的JsonProvider
package com.oracle.bcs.gateway;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class MyJsonProvider extends JacksonJaxbJsonProvider {
private static final ObjectMapper objectMapper = new ObjectMapper();
public BcsJsonProvider() {
objectMapper.setSerializationInclusion(Include.NON_NULL);
super.setMapper(objectMapper);
}
}
这个MyJsonProvider 加载了一个属性,即取消null字段。
第二步,把MyJsonProvider 注入系统
package com.xxx.xxx;
import org.glassfish.jersey.server.ResourceConfig;
public class MyResourceConfig extends ResourceConfig {
public ResourceConfig() {
packages("com.xxx.xxx");
register(MyJsonProvider.class);
}
}
第三步,把MyResourceConfig注册到web.xml
$ cat web/WEB-INF/web.xml
...
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.xxx.xxx.MyResourceConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
...
然后再次运行,就会发现null值已经被消除掉了。
$ curl http://localhost:8080/version
{"version":"v1.0"}
这个方法是不是可以?
但是,注意这个方法设置的是整个Servlet里面的所有Json对象,不是针对特定类或者特定域的。