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

{
  "data": {
    "id": 1,
    "username": "renato",
    "name": "Renato",
    "email": "[email protected]",
    "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": "[email protected]",
    "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;

}

最佳答案

我忘记将@Data放在ProgramModel和ViewModel中。

07-27 13:46