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

   在Spring3.2中返回Json字符串,过滤掉不需要的Bean中属性对应Json中key的value=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"}]


你可能感兴趣的:(注解,spring,json)