首页 > 解决方案 > SpringBoot Rest Controller中与Jackson@JsonIgnore合作MongoDB延迟加载

问题描述

RestController在我的SpringBoot应用程序中写了一个。我也在使用MongoDB。这是我的实体:

public class LocationEntity {

    @Id
    private String id;

    private String name;

    @DBRef(lazy = true)
    @JsonIgnore
    private UserEntity owner;

    private String description;

    @DBRef(lazy = true)
    private List<RoleEntity> roles;

    private Date date;

    public LocationEntity(String name, UserEntity owner, String description, List<RoleEntity> roles, Date date) {
         this.name = name;
         this.owner = owner;
         this.description = description;
         this.roles = roles;
         this.date = date;
    }
}

RoleEntityUserEntity也是来自同一数据库的实体。我RestController的方法返回ResponseEntity,所以默认Jackson是用里面序列化Object到的JSON。我想准确地询问延迟加载。如果我在序列化中使用@JsonIgnorefromJackson忽略该字段,ORM 不会从数据库中获取“惰性字段”吗?

提前感谢您的帮助!

标签: javamongodbormjacksonlazy-loading

解决方案


ORM 只会在需要时获取那些延迟加载的字段。这意味着如果您指示 Jackson@JsonIgnore在序列化期间忽略它们(使用注释),ORM 将不会获取它们。


推荐阅读