0

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.

Praytic
  • 1,771
  • 4
  • 21
  • 41
  • are you specifying on the mapper the like final String serializedValue = objectMapper.writerWithView(View.MyView.class) ?? – Damian229 Nov 19 '20 at 11:37
  • apart from that, could you add also the Defined interface/class Dummy and MyView? Only one class or interface can be specified with the @JsonView annotation, but you can use inheritance to represent JSON View hierarchies – Damian229 Nov 19 '20 at 11:48
  • They are defined in separate files as empty interface. And I don't use inheritance in that case. – Praytic Nov 19 '20 at 11:56
  • ok, which jackson library version are you using? I found another question having the same problem than you https://stackoverflow.com/a/36555400/3346298, the link is the best response for that question. Seems like the guy was using annotations from two different versions of jackson, one from spring and another one from another package – Damian229 Nov 19 '20 at 12:49
  • com.fasterxml.jackson.core:jackson-annotations:2.11.0 com.fasterxml.jackson.core:jackson-core:2.11.0 com.fasterxml.jackson.core:jackson-databind:2.11.0 com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:2.6.7 com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.10.1 com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.11.2 com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.2 com.fasterxml.jackson.module:jackson-module-jaxb-annotations:2.11.0 com.fasterxml.jackson.module:jackson-module-parameter-names:2.11.2 – Praytic Nov 19 '20 at 12:54
  • These are my jackson dependencies. – Praytic Nov 19 '20 at 12:55
  • what is the spring boot version? – Damian229 Nov 19 '20 at 14:59
  • org.springframework.boot:spring-boot:2.3.3.RELEASE – Praytic Nov 19 '20 at 15:11
  • sorry for asking so many things, trying to isolate the issue. Do you have a CustomObjectMapper defined? – Damian229 Nov 19 '20 at 15:39
  • does the controller has the annotation @restController? – Damian229 Nov 19 '20 at 15:42
  • 1
    I'm not able to reproduce your problem, I created a project to simulate the same conditions you have, but it works for me. When I request with postman the response is the expected behaviour. Here is a link to the project https://github.com/damianperezescribano/stackoverflowquestionjsonviewpraytic.git There should be something else on your configuration that you didn't post here which is making it fail. Could you share a similar project but having the issue you had? – Damian229 Nov 20 '20 at 08:42
  • Sorry but I can't provide a similar project for that matter. I suppose that the problem is in dependencies but this is not a priority for me to find out which ones. Meanwhile I will use my dirty solution. Thanks for your help. – Praytic Nov 20 '20 at 11:20
  • 1
    well, if It helps for discarding problems, the project I've uploaded on github has all the dependencies you told me, and it works. – Damian229 Nov 20 '20 at 16:52

0 Answers0