问题描述
我正在用spring
创建一个rest
服务,并想提供一个json
响应:
@RequestMapping(value = "/test",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public MyResponse content() {
return rsp;
}
MyResponse
可能包含不应在JSON
响应中返回的null
值(这些参数应该被删除)。
@XmlRootElement
class MyResponse {
}
那可能吗?
1楼
尝试这个 :
@JsonInclude(JsonInclude.Include.NON_NULL)
class MyResponse {
...
}
您需要更新您的依赖项并导入:
import com.fasterxml.jackson.annotation.JsonInclude;
2楼
全局删除null
属性。
spring.jackson.default-property-inclusion = non_null