类关系问题在 JSON 中不返回某些数据

Posted

技术标签:

【中文标题】类关系问题在 JSON 中不返回某些数据【英文标题】:Class relationship problem does not return some data in JSON 【发布时间】:2020-08-09 12:32:09 【问题描述】:

我需要将此 Json 返回到我的项目:


  "data": 
    "id": 1,
    "username": "renato",
    "name": "Renato",
    "email": "asdasd@outlook.com",
    "roles": [
      
        "id": 1,
        "name": "ROLE_USER",
        "accessList": [
          
            "id_access": 1,
            "id_role": 
              "id_role": 1,
              "name": "ROLE_USER",
              "authority": "ROLE_USER"
            ,
            "id_program": 
              "id_program": 1,
              "code_program": "TEST",
              "name": "test"
             ,
            "id_view": 
              "id_view": 1,
              "code_view": "TEST",
              "name": "test"
             ,
            "menuYesNo": true,
            "accessYesNo": true,
            "saveYesNo": true,
            "editYesNo": true,
            "deleteYesNo": true
          
        ]
      
    ]
  

但它返回这个:


  "data": 
    "id": 1,
    "username": "renato",
    "name": "Renato",
    "email": "asdasd@outlook.com",
    "roles": [
      
        "id": 1,
        "name": "ROLE_USER",
        "accessList": [
          
            "id_access": 1,
            "id_role": 
              "id_role": 1,
              "name": "ROLE_USER",
              "authority": "ROLE_USER"
            ,
            "id_program": ,
            "id_view": ,
            "menuYesNo": true,
            "accessYesNo": true,
            "saveYesNo": true,
            "editYesNo": true,
            "deleteYesNo": true
           
        ]
      
    ]
  

只有AccessModel和RoleModel类有双向关系,ProgramModel和ViewModel与AccessModel之间存在单向关系。

OBS:我使用 ModelMapper 将 UserModel 映射到 UserDTO。在 UserDTO 中存在 RoleModel。 RoleModel 和 AccessModel 分别有一个 @JsonManagedReference 和 @JsonBackReference,但 ProgramModel 和 ViewModel 没有。

@Data
@Entity
@Table(schema = "`SCH`", name = "`USER`")
public class UserModel implements UserDetails 

    private static final long serialVersionUID = -2195101536379303067L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(table = "USER", name="`ID_USER`", nullable = true)
    private Long id_user;
    @Column(table = "USER", name="`USERNAME`", nullable = true, length = 50)
    private String username;
    @Column(table = "USER", name="`PASSWORD`", nullable = true, length = 255)
    private String password;
    @Column(table = "USER", name="`NAME`", length = 255)
    private String name;
    @Column(table = "USER", name="`EMAIL`", length = 255)
    private String email;
    @Column(table = "USER", name="`DATE_EXPERED`", nullable = true)
    private LocalDate dateExpered;
    @Column(table = "USER", name="`ACCOUNT_ACTIVE`", nullable = true)
    private Boolean accountAtive;
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(schema = "`SCH`", name = "`USER_ROLE`" ,
    joinColumns = @JoinColumn(
        name = "`CD_USER`", referencedColumnName ="`ID_USER`"
    ),
    inverseJoinColumns = @JoinColumn(
        name = "`CD_ROLE`", referencedColumnName = "`ID_ROLE`"
    ))
    @JsonBackReference
    private Collection<RoleModel> roles;

    //METHODS USERDETAILS


@Data
@Entity
@Table(schema = "`SCH`", name = "`ROLE`")
public class RoleModel implements GrantedAuthority 

    private static final long serialVersionUID = -1320143054659054908L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(table = "ROLE", name = "`ID_ROLE`", nullable = true)
    private Long id_role;

    @Column(table = "ROLE", name = "`NAME`", nullable = true, length = 255)
    private String name;

    @JsonBackReference
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "id_role", fetch = FetchType.LAZY)
    private List<AccessModel> accessList;



@Data
@Entity
@Table(schema = "`SCH`", name = "`ACCESS`")
public class AccessModel implements Serializable 

    private static final long serialVersionUID = -5590889002302223720L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(table = "ACCESS", name = "`ID_ACCESS`", nullable = true)
    private Long id_access;


    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "`CD_ROLE`")
    @JsonManagedReference
    private RoleModel id_role;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "`CD_PROGRAM`")
    private ProgramModel id_program;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "`CD_VIEW`")
    private ViewModel id_view;

    @Column(table = "ACCESS", name = "`MENU_YES_NO`", nullable = true)
    private Boolean menuYesNo;

    @Column(table = "ACCESS", name = "`ACCESS_YES_NO`", nullable = true)
    private Boolean accessYesNo;

    @Column(table = "ACCESS", name = "`SAVE_YES_NO`", nullable = true)
    private Boolean saveYesNo;

    @Column(table = "ACCESS", name = "`EDIT_YES_NO`", nullable = true)
    private Boolean editYesNo;

    @Column(table = "ACCESS", name = "`DELETE_YES_NO`", nullable = true)
    private Boolean deleteYesNo;


@Entity
@Table(name = "`PROGRAM`", schema = "`SCH`")
public class ProgramModel implements Serializable 

    private static final long serialVersionUID = -726159076909575803L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(table = "PROGRAM", name = "`ID_PROGRAM`", nullable = true)
    private Long id_program;

    @Column(table = "PROGRAM", name = "`CODE_PROGRAM`", nullable = true)
    private String code_program;

    @Column(table = "PROGRAM", name = "`NAME`", nullable = true)
    private String name;

    @Column(table = "PROGRAM", name = "`ACTIVE`", nullable = true)
    private Boolean active;



@Entity
@Table(name = "`VIEW`", schema = "`SCH`")
public class ViewModel implements Serializable 

    private static final long serialVersionUID = 3900486010030569933L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(table = "VIEW", name = "`ID_VIEW`", nullable = true)
    private Long id_view;
    @Column(table = "VIEW", name = "`CODE_VIEW`", nullable = true)
    private String code_view;
    @Column(table = "VIEW", name = "`NAME`", nullable = true)
    private String name;
    @Column(table = "VIEW", name = "`ACTIVE`", nullable = true)
    private Boolean active;


【问题讨论】:

在您的问题上添加您的实体模型,然后我们可能会帮助您... 添加模型... List&lt;AccessModel&gt; accessList; 上将fetch = FetchType.LAZY 更改为fetch = FetchType.EAGER 还是一样。我在 github 上升级了我的项目:github.com/rntjr/architecture-web-system/tree/feature/…. 我使用的 GET 是:localhost:8080/api/user/userdto 对于 Auth (POST):localhost:8080/api/auth 【参考方案1】:

我忘记在 ProgramModel 和 ViewModel 中输入@Data

【讨论】:

以上是关于类关系问题在 JSON 中不返回某些数据的主要内容,如果未能解决你的问题,请参考以下文章

可视化Java框架中不同类和接口之间的关系

如何从关系表中查询数据并以json格式显示它们[重复]

数据服务返回对象时的类组合问题

类与类之间的关系

从关系数据库获取 json 数据的更好方法?

Restkit 0.24.0:如何将对象(包括对多关系)序列化为 JSON