I'm using Jersey and Jackson to build a web application. Responses will be returned by using logic similar to the code snippets below. (unrelated parts are deleted)
public Response toResponse() {
String name = // some methods to get name
Integer score = // some methods to get score
final MyDTO dto = new MyDTO(name, score);
return Response
.ok()
.encoding(StandardCharsets.UTF_8.toString())
.type(MediaType.APPLICATION_JSON_TYPE)
.entity(dto)
.build();
}
And the MyDTO
class:
@XmlRootElement
public final class MyDTO {
@NotNull final private String name;
@NotNull final private Integer score;
// constructor, getters, setters...
}
And I'm allowed to have only non-null values in MyDTO.
What I want to achieve is to hide score
field in the JSON response when the score
exactly equals to 0.
I looked into questions like here and here, but cannot manage to find a usable answer.
Example:
when John's score was 1: {"name": "John", "score": 1}
when John's score was 0: {"name": "John"}