首页 > 代码库 > Spring3.2中返回JSON去掉Bean中NULL值

Spring3.2中返回JSON去掉Bean中NULL值

   在Spring3.2中返回Json字符串,过滤掉不需要的Bean中属性对应Json中key的value=http://www.mamicode.com/null的值,可以同过在Bean中加入注解的方式来过滤


使用jackson-all-1.8.1.jar包,在需要序列化输出Json的类上添加如***解

import org.codehaus.jackson.map.annotate.JsonSerialize;

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class BookMark {
	
	private  String  markId;
	
	private  String title;
	
	private String umName;
	
	private String node;

	public String getMarkId() {
		return markId;
	}

	public void setMarkId(String markId) {
		this.markId = markId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getNode() {
		return node;
	}

	public void setNode(String node) {
		this.node = node;
	}

	public String getUmName() {
		return umName;
	}

	public void setUmName(String umName) {
		this.umName = umName;
	}
	
}

加入注解之前

[{"umName":null,"markId":"3","title":"书签1","node":null},
{"umName":null,"markId":"4","title":"书签2","node":null},
{"umName":null,"markId":"5","title":"书签3","node":null},
{"umName":null,"markId":"6","title":"书签5","node":null},
{"umName":null,"markId":"7","title":"书签4","node":null}]

加入注解之后

[{"markId":"3","title":"书签1"},{"markId":"4","title":"书签2"},
 {"markId":"5","title":"书签3"},{"markId":"6","title":"书签5"},
 {"markId":"7","title":"书签4"}]


本文出自 “脚踏实地,仰望星空” 博客,请务必保留此出处http://xubcing.blog.51cto.com/3502962/1597220

Spring3.2中返回JSON去掉Bean中NULL值