Jackson 序列化期间子对象为空?

发布于 2025-01-08 06:39:34 字数 3727 浏览 0 评论 0原文

我捕获了 json 响应,我发现它是

{"page":"1","total":"2","records":"15","rows":[{"id":1,"firstName":"John","lastName":"Smith","username":"john","password":"21232f297a57a5a743894a0e4a801fc3","role":null},{"id":2,"firstName":"Jane","lastName":"Adams","username":"jane","password":"ee11cbb19052e40b07aac0ca060c23ee","role":null},

My User 和 Role 类,非常简单,所以我想我一定忽略了一些琐碎的事情。

@Entity(name="user")
public class User implements Serializable {
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    private String firstName;
    private String lastName;
    
    @Column(unique=true)
    private String username;
    private String password;
    
    @JsonIgnore
    @OneToMany(mappedBy="user", cascade={CascadeType.PERSIST, CascadeType.MERGE},fetch =FetchType.EAGER)
    private List<Role> roles =  new ArrayList<Role>();
    
    
    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    
    @ManyToOne
    private Role role;

    public User() {}
    
    public User(String username, String password, String firstName, String lastName, Role role) {
        this.username = username;
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
        this.role = role;
    }
    
    public User(String username, String firstName, String lastName, Role role) {
        this.username = username;
        this.firstName = firstName;
        this.lastName = lastName;
        this.role = role;
    }

    public User(String username) {
        this.username = username;
    }
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }
}

@Entity(name="role")
public class Role  implements Serializable{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    @JsonIgnore
    @ManyToOne
    @JoinColumn (name = "user_id", nullable= true)
    private User user;
    private Integer role;
    
    public Role() {}
    
    public Role(Integer role) {
        this.role = role;
    }
    
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public Integer getRole() {
        return role;
    }
    public void setRole(Integer role) {
        this.role = role;
    }
}

我还尝试了

@JsonUnwrapped @ManyToOne private Role role;

但这更糟糕,因为在这种情况下我的 json 根本没有该角色。

{"id":1,"firstName":"约翰","lastName":"史密斯","用户名":"约翰","密码":"21232f297a57a5a743894a0e4a801fc3"},

I captured the json response and I see that it is

{"page":"1","total":"2","records":"15","rows":[{"id":1,"firstName":"John","lastName":"Smith","username":"john","password":"21232f297a57a5a743894a0e4a801fc3","role":null},{"id":2,"firstName":"Jane","lastName":"Adams","username":"jane","password":"ee11cbb19052e40b07aac0ca060c23ee","role":null},

My User and Role classes are super simple, so I think I must be overlooking something trivial.

@Entity(name="user")
public class User implements Serializable {
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    private String firstName;
    private String lastName;
    
    @Column(unique=true)
    private String username;
    private String password;
    
    @JsonIgnore
    @OneToMany(mappedBy="user", cascade={CascadeType.PERSIST, CascadeType.MERGE},fetch =FetchType.EAGER)
    private List<Role> roles =  new ArrayList<Role>();
    
    
    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    
    @ManyToOne
    private Role role;

    public User() {}
    
    public User(String username, String password, String firstName, String lastName, Role role) {
        this.username = username;
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
        this.role = role;
    }
    
    public User(String username, String firstName, String lastName, Role role) {
        this.username = username;
        this.firstName = firstName;
        this.lastName = lastName;
        this.role = role;
    }

    public User(String username) {
        this.username = username;
    }
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }
}

@Entity(name="role")
public class Role  implements Serializable{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    @JsonIgnore
    @ManyToOne
    @JoinColumn (name = "user_id", nullable= true)
    private User user;
    private Integer role;
    
    public Role() {}
    
    public Role(Integer role) {
        this.role = role;
    }
    
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public Integer getRole() {
        return role;
    }
    public void setRole(Integer role) {
        this.role = role;
    }
}

I tried also

@JsonUnwrapped @ManyToOne private Role role;

but that's even worse because in this case my json doesn't have the role at all.

{"id":1,"firstName":"John","lastName":"Smith","username":"john","password":"21232f297a57a5a743894a0e4a801fc3"},

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够钟 2025-01-15 06:39:34

由于复杂的遗留关系要求,即用户具有一个和多个角色,在将其作为响应传回之前,我错误地操作了该对象。

@JsonUnwrapped 仅当子对象中有属性时才能展开,因此它按预期工作。

I was manipulating the object incorrectly before passing it back as a response due to a complex legacy relationship requirement i.e. User has a role and roles.

@JsonUnwrapped can only unwrap if there are properties in the child object, so it works as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文