![](/img/trans.png)
[英]How convert an annotation (all its diff properties) into a JSON object using Jackson/ObjectMapper?
[英]Jackson ObjectMapper ignore all properties that has no annotation
我的目標是將 jsonObject 轉換為 Class。 我只想添加在類中注釋的字段。 示例:json 對象包含 50 個字段。 類有 4 個字段。 我只想映射精確的 4 個字段,而不在課堂上添加 46 個加法忽略。
JSON:
{
"id": "1",
"name": "John",
"Address": "Some Address 7009",
}
班級:
public static class User {
Integer id;
String name;
public User (@JsonProperty("id")Integer id, @JsonProperty("name")String name {
this.id= id;
this.name= name;
}
....
}
用戶類沒有地址字段。 我的目標是排除它,因為它沒有注釋。
使用@JsonIgnoreProperties
注釋您的類,如下所示:
@JsonIgnoreProperties(ignoreUnknown = true)
public class User {
...
}
當ignoreUnknown
為true
,所有無法識別的屬性(即,沒有接受它們的設置者或創建者)都將被忽略而不會發出警告(盡管未知屬性的處理程序,如果有,仍將被調用),無一例外。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.