显示空属性 json 响应 spring 数据休息?
show null attributes json response spring data rest?
假设我有一个这样的实体。
public class Person{
Long Id,
String name,
String city,
Long age
//getters, setters, constructor
}
当我创建存储库并使用 GET 请求输出城市条目时为空,下面是我的 json 响应。
{
"name": "jon",
"age": 34
}
但我想要这个。
{
"name": "jon",
"city": null,
"age": 34
}
即显示空属性。
最简单的解决方法是什么?
我认为您应该检查 json 注释 JsonInclude.Include 并将其设置为 ALWAYS :
https://fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/fasterxml/jackson/annotation/JsonInclude.Include.html
确保您没有您的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
。
或者,将您的 class 注释如下:
@JsonInclude(Include.ALWAYS)
public class Person {
...
}
假设我有一个这样的实体。
public class Person{
Long Id,
String name,
String city,
Long age
//getters, setters, constructor
}
当我创建存储库并使用 GET 请求输出城市条目时为空,下面是我的 json 响应。
{
"name": "jon",
"age": 34
}
但我想要这个。
{
"name": "jon",
"city": null,
"age": 34
}
即显示空属性。
最简单的解决方法是什么?
我认为您应该检查 json 注释 JsonInclude.Include 并将其设置为 ALWAYS : https://fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/fasterxml/jackson/annotation/JsonInclude.Include.html
确保您没有您的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
。
或者,将您的 class 注释如下:
@JsonInclude(Include.ALWAYS)
public class Person {
...
}