@JsonProperty和@JSONField 使用
@JsonProperty和@JSONField注解的区别
1.底层框架不同 @JsonProperty 是Jackson实现的 @JSONField 是fastjson实现的
2.用法不同 (1)bean序列化为Json:
@JsonProperty: ObjectMapper().writeValueAsString(Object value)
@JSONField: ObjectMapper().readValue(String content, Class valueType)
(2)Json反序列化为bean:
@JsonProperty:ObjectMapper().readValue(String content, Class valueType)
@JSONField:JSONObject.parseObject(String content, Class valueType)
(3)作用域
@JSONproperty 注解用于属性上面 可以在属性名上面增加@JsonProperty(value=“name”)。
import com.alibaba.fastjson.annotation.JSONField;public class MyClass {@JSONField(name = "custom_name")private String myField;// getter and setter
}
@JSONField 注解可以用于get、set以及属性上面
可以在get/set/属性名上面增加@JSONField(value=“name”)。
import com.fasterxml.jackson.annotation.JsonProperty;public class MyClass {@JsonProperty("custom_name")private String myField;// getter and setter
}