簡體   English   中英

如何刪除 json 休息響應中的空參數?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM