The difference and usage of @JsonProperty and @JSONField annotations

The difference and usage of @JsonProperty and @JSONField annotations

1. Purpose

All are to solve the inconsistency between bean and json fields, or the field definition does not meet the standards required by the front end

2. Difference

1. The framework is different:

@JsonProperty is Jackson's package, while @Jsonfield is fastjson's package

2. Different usage:

(1) The bean is converted into a Json string:

  • @JsonProperty:ObjectMapper().writeValueAsString(Object value)
  • @JSONField:ObjectMapper().readValue(String content, Class valueType)

(2) Convert Json string to bean:

  • @JsonProperty:ObjectMapper().readValue(String content, Class valueType)
  • @JSONField:JSONObject.parseObject(String content, Class valueType)

(3) The @JSONField annotation can be used on get, set and properties

public class Test{
    
    
	
	/*
		注解在属性上的时候可以设置一些序列化、格式化的属性
		@JSONField(serialize = false)---->序列化的时候忽略这个属性
		@JSONField(format = "yyyyMMdd")---->序列化和反序列化额时候使用yyyyMMdd,一般在Date类型的字段上使用
	*/	
	@JSONField(serialize = false)
	public String Name;
	/*
		注解在set方法表示json转成bean的时候会将json中的Name属性赋值给实体类的Name
	*/
	@JSONField(name="Name")
	public void setName(String Name){
    
    
		this.Name = Name;
	}
	/*
		注解在get方法表示bean转换成json的时候会把实体类的Name属性值赋给json的Name
	*/
	@JSONField(name="Name")
	public String getName(){
    
    
		return this.Name;
	}
}

(3) The @JSONproperty annotation is used on the property

To serialize the trueName attribute into name, you can add @JsonProperty(value="name") on the attribute name.

(4) Jackson's @JsonIgnore is used

Function: Some properties in the java bean are ignored during json serialization, and both serialization and deserialization are affected.

How to use: Generally marked on the attribute or method, the returned json data does not contain this attribute.

Pitfalls of JsonField and jsonProperty annotations

Problems encountered:

The fields in the entity class are generally lowercase, but the json data to be transmitted (received and sent) requires the first letter of the field to be capitalized

public class Test{
    
    
    private String Name;------>setter + getter
}
json:
{
    
    
    'Name': 'test'
}

But if you receive it directly in this way, you will not receive the value of Name

So you need to use jsonField annotation and jsonProperty annotation

@JSONField

This annotation can be annotated on the properties, setter and getter methods of the entity class respectively

public class Test{
    
        
    /*
        注解在属性上的时候可以设置一些序列化、格式化的属性
        @JSONField(serialize = false)---->序列化的时候忽略这个属性
        @JSONField(format = "yyyyMMdd")---->序列化和反序列化额时候使用yyyyMMdd,一般在Date类型的字段上使用
    */    
    @JSONField(serialize = false)
    public String Name;
    /*
        注解在set方法表示json转成bean的时候会将json中的Name属性赋值给实体类的Name
    */
    @JSONField(name="Name")
    public void setName(String Name){
    
    
        this.Name = Name;
    }
    /*
        注解在get方法表示bean转换成json的时候会把实体类的Name属性值赋给json的Name
    */
    @JSONField(name="Name")
    public String getName(){
    
    
        return this.Name;
    }
}

@JSONProperty

( 1 ) This annotation is used on the attribute, and its function is to serialize the name of the attribute into another name,

(2) If the trueName property is serialized as name, @JsonProperty(value="name")

(3) Similar to the @JSONField annotation on the setter method

Guess you like

Origin blog.csdn.net/qq_43842093/article/details/131024536