Controller中与Jackson

Controller中与Jackson

本文介绍了在SpringBoot Rest Controller中与Jackson @JsonIgnore合作进行MongoDB延迟加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have written a RestController in my SpringBoot app. I am using a MongoDB as well. This is my entity:

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,因此默认情况下,内部使用JacksonObject序列化为JSON.我想确切地问一下惰性加载.如果我使用Jackson中的@JsonIgnore来忽略序列化中的该字段,那么ORM不会从数据库中获取惰性字段" 吗?

RoleEntity and UserEntity are entities from the same database as well.My RestController's methods return ResponseEntity, so by default Jackson is used inside to serialize Object to JSON.I would like to ask about lazy loading precisely. If I use @JsonIgnore from Jackson to ignore that field in serialization, will ORM not get "lazy fields" from database?

谢谢您的帮助!

推荐答案

ORM仅在需要时才提取那些延迟加载的字段.这意味着,如果您指示Jackson在序列化过程中忽略它们(使用@JsonIgnore注释),那么ORM将不会获取它们.

ORM will only fetch those lazy loaded fields when needed. That means if you instruct Jackson to ignore them (using @JsonIgnore annotation) during serialization, ORM will not fetch them.

这篇关于在SpringBoot Rest Controller中与Jackson @JsonIgnore合作进行MongoDB延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 05:04