Java中jackson的注解@JsonProperty、@JsonIgnore、@JsonIgnoreProperties、@JsonFormat、@JSONField
									
 
下文笔者讲述jackson注解@JsonProperty、@JsonIgnore、@JsonIgnoreProperties、@JsonFormat、@JSONField的简介说明,如下所示
				 
				@JsonProperty、@JsonIgnore、@JsonIgnoreProperties、@JsonFormat、@JSONField注解
@JsonProperty、@JsonIgnore、@JsonIgnoreProperties、@JsonFormat、@JSONField注解位于 com.fasterxml.jackson.annotation包中
注解使用方法
引入依赖
<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
        <artifactId>jackson-databind</artifactId> 
    <version>2.5.3</version>
</dependency>    
@JsonProperty、@JsonIgnore、@JsonIgnoreProperties、@JsonFormat、@JSONField注解的使用方法
一、@JsonProperty
 方式1:全写,使用“=”;
@JsonProperty(value = "", required = true)
方式2:简写,直接把value写到括号里,required默认为false。
@JsonProperty("")
点击并拖拽以移动
value属性:代表该属性序列化和反序列化的时候的key值。
required属性:默认false,例如当required=false的时候,当反序列化的时候没有找到key值,就会报错。
二、@JsonIgnore
一般标记在属性或者方法上,返回的json数据即不包含该属性
三、@JsonIgnoreProperties
类注解,作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响。
四、@JsonFormat
用于属性或者方法上(最好是属性上),可以方便的把Date类型直接转化为我们想要的模式
写法:@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GTM+8")
五、@JSONField
主要用于解析前端传过来的时间格式的数据
用法:@JSONField(format = "yyyy-MM-dd")
@JsonProperty、@JsonIgnore、@JsonIgnoreProperties、@JsonFormat、@JSONField注解示例
 
实体类Student
@JsonIgnoreProperties(value = { "address", "score" })
public class Student implements Serializable {
	private static final long serialVersionUID = 1L;
	@JsonProperty(value = "name", required = true)
	private String trueName;
	@JsonIgnore
	private int age;
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GTM+8")
	private Date birthday;
	private String address;
	private String score;
	public String getTrueName() {
		return trueName;
	}
	public void setTrueName(String trueName) {
		this.trueName = trueName;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getScore() {
		return score;
	}
	public void setScore(String score) {
		this.score = score;
	}
	@Override
	public String toString() {
		return "name: " + this.trueName + ", address: " + this.address + ", age:" 
				 + this.age + ", birthday:" + this.birthday + ", score:" + this.score;
	}
} 
public class TestJsonProperty {
	public static void main(String[] args) throws IOException {
		Student student = new Student();
		student.setTrueName("猫猫");
		student.setAge(19);
		student.setBirthday(new Date());
		student.setAddress("深圳市");
		student.setScore("120");
		// 使用writeValuesAsString把对象转化成json字符串。
		String stuJson = new ObjectMapper().writeValueAsString(student);
		System.out.println(stuJson);
		
		//使用readValue把字符串转化为对象
		Student stu = new ObjectMapper().readValue(stuJson, Student.class);
		System.out.println(stu.toString());
	}
}
									
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

 
			 
                
                
                
               
 
          

