@JsonProperty和@JSONField注解的區(qū)別解析(最新)
JSON(JavaScript Object Notation)
json是一種常見的數(shù)據(jù)交換的輕量級數(shù)據(jù)格式。HTTP協(xié)議傳輸數(shù)據(jù)可以有多種數(shù)據(jù)格式,比如下面幾種常見數(shù)據(jù)傳輸格式,除此之外還有其他的數(shù)據(jù)交換格式。
數(shù)據(jù)傳輸類型 | 編碼類型 | 示例 |
---|---|---|
表單格式 | application/x-www-form-urlencoded | username=zk&password=123 |
JSON(JavaScript Object Notation) | application/json | {"username": "zk","password": "123"} |
XML(eXtensible Markup Language) | application/xml | <user><username>zk</username><password>123</password></user> |
Jackson
Jackson是一款優(yōu)秀的JSON解析庫,添加了依賴之后就可以使用對應(yīng)的注解,讓我們能夠自由的將Java對象和JSON做轉(zhuǎn)換。
比如Java對象轉(zhuǎn)JSON
@JsonProperty和@JSONField
為了解決JSON字符串和其實(shí)體bean的屬性名匹配不上的問題,@JsonProperty和@JSONField都可以將某一屬性名序列化為另一屬性名。
那么@JsonProperty和@JSONField有什么區(qū)別呢?
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.annotation.JSONField; import com.fasterxml.jackson.annotation.JsonProperty; import com.hust.zhang.serializable.JsonUtils; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; public class JsonPropertiesVsJsonField { @AllArgsConstructor @Data @Builder static class Properties { @JsonProperty(value = "json-properties") private String jsonProperties; @JSONField(name = "json-field") private String jsonField; } public static void main(String[] args) { Properties properties = Properties.builder() .jsonProperties("test-properties") .jsonField("test-field") .build(); System.out.println(JsonUtils.toJson(properties)); System.out.println(JSON.toJSONString(properties)); } }
輸出結(jié)果如下,
{"jsonField":"test-field","json-properties":"test-properties"}
{"json-field":"test-field","jsonProperties":"test-properties"}
可以看到調(diào)用JsonUtils.toJson方法時,加了@JsonProperty才與bean實(shí)際屬性名匹配。
其中該方法定義如下,ObjectMapper的writeValueAsString方法。
/** * Json轉(zhuǎn)換工具類 */ @Slf4j public final class JsonUtils { /** * 私有無參構(gòu)造方法 常量類不能實(shí)例化,直接引用 */ private JsonUtils() { } private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); static { OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); OBJECT_MAPPER.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(), true); } public static String toJson(Object object) { try { return OBJECT_MAPPER.writeValueAsString(object); } catch (JsonProcessingException e) { log.error("Failed to write the object to string" + object.getClass().getName()); return null; } } public static <T>T parse(String json, Class<T> tClass){ try { return OBJECT_MAPPER.readValue(json,tClass); } catch (JsonProcessingException e) { log.error("Failed to deserialize JSON content, json value : " + json); return null; } } }
除此之外還可以看看@JsonAlias
注解。
參考鏈接
1、https://baijiahao.baidu.com/s?id=1765042798858921947&wfr=spider&for=pc
2、https://developer.aliyun.com/article/768691
到此這篇關(guān)于@JsonProperty和@JSONField注解的區(qū)別的文章就介紹到這了,更多相關(guān)@JsonProperty和@JSONField注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
區(qū)分java中String+String和String+char
這篇文章主要向大家詳細(xì)區(qū)分了java中String+String和String+char,感興趣的小伙伴們可以參考一下2016-01-01java 算法之歸并排序詳解及實(shí)現(xiàn)代碼
這篇文章主要介紹了java 算法之歸并排序詳解及實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03Java實(shí)現(xiàn)世界上最快的排序算法Timsort的示例代碼
Timsort?是一個混合、穩(wěn)定的排序算法,簡單來說就是歸并排序和二分插入排序算法的混合體,號稱世界上最好的排序算法。本文將詳解Timsort算法是定義與實(shí)現(xiàn),需要的可以參考一下2022-07-07mybatis使用foreach遍歷list集合或者array數(shù)組方式
這篇文章主要介紹了mybatis使用foreach遍歷list集合或者array數(shù)組方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07