![](/img/trans.png)
[英]Spring REST Service: how to configure to remove null objects in json response
[英]How to remove null parameters in a json rest response?
我正在用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 {
}
那可能嗎?
嘗試這個 :
@JsonInclude(JsonInclude.Include.NON_NULL)
class MyResponse {
...
}
您需要更新您的依賴項並導入:
import com.fasterxml.jackson.annotation.JsonInclude;
全局刪除null
屬性。
spring.jackson.default-property-inclusion = non_null
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.