Apache Camel Swagger - 使用 JPA 实体作为休息类型

Apache Camel Swagger - using JPA entity as rest type

我有这个 Rest-DSL:

// this api creates new user
rest("/user")
    .post()
    .type(User.class).to("jpa://com.project.User")

这是我的实体:

public class User{
    @Id
    private String id;
    @ManyToOne
    @JoinColumn(name = "id_role")
    private Role role;
}

public class Role{
    @Id
    private String id;
    @OneToMany(mappedBy="user")
    private List<User> users;
}

我的问题出在我在 Body 值参数示例中的招摇上。它包含这样的内容:

{
  "id": "string",
  "role": {
    "id": "string",
    "users": [
      {
        "id": "string",
        "roles": [
          {}
        ]
      }
    ]
  }
}

相当复杂,虽然我只需要 idid_role 参数来创建 (POST) 新用户。我希望正文示例显示如下:

{
  "id": "string",
  "id_role": "string" 
}

我意识到我的实体创建不正确。这些是我学到的:

  1. Configure CascadeType in associated JPA entities

    @Entity
    
    public class User {
        @Id
        private String id;
        @ManyToOne
        @JoinColumn(name = "id_role")
        private Role role;
    }
    
    @Entity
    public class Role{
        @Id
        private String id;
        @OneToMany(mappedBy="user", cascade = CascadeType.ALL)
        private List<User> users;
    }
    
  2. 使class不递归,设置@JsonIgnore

    @Entity
    @JsonIdentityInfo(
          generator = ObjectIdGenerators.PropertyGenerator.class, 
          property = "id")
    public class User{
        @Id
        private String id;
        @ManyToOne
        @JoinColumn(name = "id_role")
        private Role role;
    }
    
    @Entity
    @JsonIdentityInfo(
          generator = ObjectIdGenerators.PropertyGenerator.class, 
          property = "id")
    public class Role{
        @Id
        private String id;
        @OneToMany(mappedBy="user", cascade = CascadeType.ALL)
        @JsonIgnore
        // this attribute will not appear inside Role class
        private List<User> users;
    }