![](/img/trans.png)
[英]Spring REST Service: how to configure to remove null objects in json response
[英]show null attributes json response spring data rest?
想象我有一個這樣的實體。
public class Person{
Long Id,
String name,
String city,
Long age
//getters, setters, constructor
}
當我創建存儲庫並使用GET請求輸出的城市條目為null時,以下是我的json響應。
{
"name": "jon",
"age": 34
}
但是我想要這個。
{
"name": "jon",
"city": null,
"age": 34
}
即顯示空屬性。
最簡單的解決方法是什么?
確保您的ObjectMapper
沒有以下配置:
mapper.setSerializationInclusion(Include.NON_NULL);
如果有,請將其刪除或更改為Include.ALWAYS
。
還要檢查您的application.properties
。 如果您使用的是Spring Boot 1.3,則通過spring.jackson.serialization-inclusion
屬性配置序列化包含。
Jackson 2.7和Spring Boot 1.4使用名為spring.jackson.default-property-inclusion
。
確保此類屬性的值為non_null
。
或者,對您的班級進行如下注釋:
@JsonInclude(Include.ALWAYS)
public class Person {
...
}
我認為您應該檢查JSON注釋JsonInclude.Include並將其設置為ALWAYS: https ://fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/fasterxml/jackson/annotation/JsonInclude.Include.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.