【问题标题】:Composite object not showing up in the response - Spring Boot复合对象未显示在响应中 - Spring Boot
【发布时间】:2021-12-01 02:43:18
【问题描述】:

所以我在我的系统中引入了一些角色。我有一个看起来像这样的课程:

@Entity
@Table(name = "user_role_mapping")
@AllArgsConstructor
@NoArgsConstructor
@Data
public class UserRoleMapping {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    Integer id;

    @Column(name = "role_id")
    Role role;

    @ManyToOne
    @JoinColumn(name = "user_id")
    User user;

    public UserRoleMapping(Role role, User user) {
        this.role = role;
        this.user = user;
    }
}

由此映射的类如下所示:

public enum Role {
    ROLE_CUSTOMER(1) , ROLE_ADMIN(0);

    public int id;

    private Role (int id){
    }
}

用户类看起来像:

public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "phone")
    private String phoneNumber;

    @Column(name = "name")
    private String name;

    @OneToMany(mappedBy = "id", fetch = FetchType.EAGER,
            cascade = CascadeType.ALL)
    private Set<UserRoleMapping> userRoles;
}

对于初始 POC,我编写了一个简单的 API 方法,如下所示:

@RequestMapping(value = "", method = RequestMethod.GET)
        public ResponseEntity<?> returnUserAndDefaultRole( @RequestParam(name = "phone") String phone) {
           User user = usersRepository.findByPhoneNumber(phone);
           List<UserRoleMapping> urm = userRoleMappingRepository.findAllByUser(user);
        UserRoleMapping userRoleMapping;
        if (urm.size() == 0) {
            userRoleMapping = new UserRoleMapping(Role.ROLE_CUSTOMER, user);
            userRoleMappingRepository.save(userRoleMapping); }
        else {
            userRoleMapping  = urm.get(0);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("User", user );
        result.put("Role_info" ,userRoleMapping);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

在这里,我希望在Role_info 键下获得同样具有 Set 和一个 userRoleMapping 对象的 User 对象。

我的实际输出如下:

    "User": {
        "id": 1542275,
        "phoneNumber": "9527725710",
        "name": "njari",
       
        "userRoles": []
    },
    "Role_info": {
        "id": 1,
        "role": "ROLE_CUSTOMER",
        "user": {
            "id": 1542275,
             "phoneNumber": "9527725710",
            "name": "njari",
            "userRoles": []
        }
    }
}

我的 Role_Info 已填充。但是,用户对象中的 userRoles 不是。 首先,我希望填充内部的用户角色集。 其次,我也不希望 Role_Info 包含整个 User 对象,我该如何实现?

【问题讨论】:

    标签: java json spring-boot jackson


    【解决方案1】:
    1. 如果我没有错,请尝试更改您的映射

       @OneToMany(mappedBy = "id", fetch = FetchType.EAGER,
                       cascade = CascadeType.ALL)
       private Set<UserRoleMapping> userRoles;
      

    收件人:

    @OneToMany(mappedBy = "user", fetch = FetchType.EAGER,
                cascade = CascadeType.ALL)
        private Set<UserRoleMapping> userRoles;
    
    1. 为了排除字段/对象,我使用@JsonIgnore:

       @JsonIgnore
       @ManyToOne
       @JoinColumn(name = "user_id")
       User user;
      

    【讨论】:

    • 这解决了我的问题!谢谢 :)。对于任何寻找这个的人 - 我还必须删除 Lombok 的 AtData 并改用 AtGetter , AtSetter 。我是从stackoverflow.com/questions/17445657/… 那里得到的。
    【解决方案2】:

    首先,我建议在带有@Transnational 注解的服务方法中使用存储库,而不是在控制器中。

    其次,您需要将新对象UserRoleMapping添加到User userRoles并保存User(cascade = CascadeType.ALL)。我建议改变

    @OneToMany(mappedBy = "id", fetch = FetchType.EAGER,
            cascade = CascadeType.ALL)
    private Set<UserRoleMapping> userRoles;
    

    @OneToMany(mappedBy = "id", fetch = FetchType.EAGER,
            cascade = CascadeType.ALL)
    private Set<UserRoleMapping> userRoles = new HashSet<>();
    

    【讨论】:

    • 是的,我提到这是一个初始 POC。这种改变对我不起作用。不过,谢谢。
    猜你喜欢
    • 2022-01-09
    • 2018-11-11
    • 2019-08-04
    • 2021-10-04
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    相关资源
    最近更新 更多