V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
coolair
V2EX  ›  问与答

@JsonIgnore 在 Spring Boot 3.1.4 中无效了吗?

  •  
  •   coolair · 197 天前 · 338 次点击
    这是一个创建于 197 天前的主题,其中的信息可能已经有所发展或是发生改变。

    使用了 JPA 、Lombok 代码如下:

    @Data
    @Entity
    public class User {
        private List<Role> roles;
    }
    
    @Data
    @Entity
    public class Role {
        @JsonIgnore
        @ManyToMany(mappedBy = "roles")
        List<User> users;
    }
    

    同样的代码在 Spring Boot 2.7.16 中没问题,在 Spring Boot 3.1.4 中就不行了,是哪里没写对吗?

    coolair
        1
    coolair  
    OP
       197 天前
    上面代码不完整。

    ```
    @Data
    @Entity
    public class User {
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
    name = "user_role",
    joinColumns = @JoinColumn(name = "user_id"),
    inverseJoinColumns = @JoinColumn(name = "role_id")
    )
    private List<Role> roles;
    }

    @Data
    @Entity
    public class Role {
    @JsonIgnore
    @ManyToMany(mappedBy = "roles")
    List<User> users;
    }
    ```

    刚在 Spring Boot 2.7.16 测了,也不行。貌似只能用 `@Transient` 了?
    Jwyt
        2
    Jwyt  
       197 天前
    这不是 jackson 的注解么
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3012 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 08:11 · PVG 16:11 · LAX 01:11 · JFK 04:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.