Consider following example:
@JsonView(MyView.class)
@SneakyThrows
@GetMapping(path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public List<DTO> get(@PathVariable Integer id) {
return service.getAllById(id);
}
This controller method should return a list of DTOs. Each DTO has fields annotated with @JsonView
annotation
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class DTO {
private Integer id;
@JsonView(MyView.class)
private String name;
}
I am using spring boot, so when I set spring.jackson.mapper.DEFAULT_VIEW_INCLUSION
to false
, the response for that endpoint is just an empty array []
. Although when I set this property to true
I am getting an array of objects with ALL fields instead of those which are marked with @JsonView
. What am I missing here?
DIRTY SOLUTION:
I was able to fix the problem by specifying dummy annotation on the class notation like that:
@Data
@JsonView(Dummy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class DTO {
private Integer id;
@JsonView(MyView.class)
private String name;
}
This way all fields which are not marked with @JsonView(MyView.class)
are marked with @JsonView(Dummy.class)
, thus they will not be affected by the silly implementation of the default view exclusion.