JsonView - default mode definition

I am working on a Spring boot application (MVC, JPA) and it should return different attributes for different requests. I found the @JsonView annotation and it seems to work. But do I need to annotate each attribute with a basic view?

Example:

Entity1

 @Entity
    public class Entity1 implements Serializable {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;

      @JsonView(JsonViews.ExtendedView.class)
      private String name;

      @OneToMany(cascade = CascadeType.ALL, mappedBy = "entity1", fetch = FetchType.EAGER)
      List<Entity2> entities2;

      @JsonView(JsonView.ExtendedView.class)
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "entity1", fetch = FetchType.LAZY)
      List<Entity3> entities3;

    }

entity2

@Entity
public class Entity2 implements Serializable {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private String name;
}

Entity3

@Entity
public class Entity3 implements Serializable {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private String name;
}

Views

public class JsonViews {
  public static class BasicView { }
  public static class ExtendedView extends BasicView { }
}

controller

@RequestMapping(method = RequestMethod.GET)
  @JsonView(JsonViews.BasicView.class)
  public @ResponseBody List<Entity1> index() {

    return repositoryEntity1.findAll();

  }

This is a cropped example, but I think this relates to the problem. I expect the controller to return identifiers and a list of objects Entity2. But it returns an empty object with "No properties." If I annotate every attribute of every class participating in this query, it seems to work, but is this really needed or a better solution? Is there any way to define "DefaultView"?

thank

: JpaRepository, , Entity3.

+4
1

, .

spring.jackson.mapper.default-view-inclusion=true

application.properties. @JsonView , .

BasicView.

+4

All Articles