Jackson 注解 -- 忽略字段

– Start

点击此处观看本系列配套视频。


如果在读写 JSON 时,我们想忽略某些字段,我们可以使用下面的注解。

@JsonIgnore

package shangbo.jackson.demo7;

import java.util.Date;

import org.apache.commons.lang3.builder.ToStringBuilder;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class Person {
	private String firstname;
	private String lastname;
	
	@JsonIgnore
	private Date brithday;

	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public Date getBrithday() {
		return brithday;
	}

	public void setBrithday(Date brithday) {
		this.brithday = brithday;
	}

}

@JsonIgnoreProperties

package shangbo.jackson.demo8;

import java.util.Date;

import org.apache.commons.lang3.builder.ToStringBuilder;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties({"brithday"})
public class Person {
	private String firstname;
	private String lastname;
	private Date brithday;

	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public Date getBrithday() {
		return brithday;
	}

	public void setBrithday(Date brithday) {
		this.brithday = brithday;
	}

}

@JsonIgnoreType

package shangbo.jackson.demo9;

import org.apache.commons.lang3.builder.ToStringBuilder;

import com.fasterxml.jackson.annotation.JsonIgnoreType;

@JsonIgnoreType
public class Address {
	private String state;
	private String city;
	private String streat;

	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getStreat() {
		return streat;
	}

	public void setStreat(String streat) {
		this.streat = streat;
	}

}
package shangbo.jackson.demo9;

import org.apache.commons.lang3.builder.ToStringBuilder;

public class Person {
	private String firstname;
	private String lastname;
	private Address address;

	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}
}

– 更多参见:Jackson 精萃
– 声 明:转载请注明出处
– Last Updated on 2019-05-25
– Written by ShangBo on 2019-05-25
– End

你可能感兴趣的:(Jackson)