简体   繁体   中英

How to remove null parameters in a json rest response?

I'm creating a rest service with spring and want to offer a json response:

@RequestMapping(value = "/test",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public MyResponse content() {
    return rsp;
}

MyResponse may contain null values which should not be returned in the JSON response (these params should just be removed).

@XmlRootElement
class MyResponse {
}

Is that possible?

Try this :

@JsonInclude(JsonInclude.Include.NON_NULL)
class MyResponse {
...
}

You'll need to update your dependencies and import this :

import com.fasterxml.jackson.annotation.JsonInclude;

全局删除null属性。

spring.jackson.default-property-inclusion = non_null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM