亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在spring-boot數據休息的POST json中傳遞@EmbeddedId

如何在spring-boot數據休息的POST json中傳遞@EmbeddedId

烙印99 2022-06-30 10:28:44
我已經使用 Spring Boot Data REST 構建了一個 REST API。我正在使用 EmbeddedId,并且還實現了 BackendIdConverter。下面是我的可嵌入類@Embeddablepublic class EmployeeIdentity implements Serializable {    @NotNull    @Size(max = 20)    private String employeeId;    @NotNull    @Size(max = 20)    private String companyId;    public EmployeeIdentity() {}    public EmployeeIdentity(String employeeId, String companyId) {        this.employeeId = employeeId;        this.companyId = companyId;    }    public String getEmployeeId() {        return employeeId;    }    public void setEmployeeId(String employeeId) {        this.employeeId = employeeId;    }    public String getCompanyId() {        return companyId;    }    public void setCompanyId(String companyId) {        this.companyId = companyId;    }    @Override    public boolean equals(Object o) {        if (this == o) return true;        if (o == null || getClass() != o.getClass()) return false;        EmployeeIdentity that = (EmployeeIdentity) o;        if (!employeeId.equals(that.employeeId)) return false;        return companyId.equals(that.companyId);    }    @Override    public int hashCode() {        int result = employeeId.hashCode();        result = 31 * result + companyId.hashCode();        return result;    }}這是我的員工模型@Entity@Table(name = "employees")public class Employee {    @EmbeddedId    private EmployeeIdentity id;    @NotNull    @Size(max = 60)    private String name;    @NaturalId    @NotNull    @Email    @Size(max = 60)    private String email;    @Size(max = 15)    @Column(name = "phone_number", unique = true)    private String phoneNumber;    public Employee() {}    public Employee(EmployeeIdentity id, String name, String email, String phoneNumber) {        this.id = id;        this.name = name;        this.email = email;        this.phoneNumber = phoneNumber;    }    public EmployeeIdentity getId() {        return id;    }    public void setId(EmployeeIdentity id) {        this.id = id;    }
查看完整描述

2 回答

?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

這是另一個解決方案。(雖然仍然不完美。)


公開 Employee 類的 id:


@Configuration

  protected class MyRepositoryRestConfigurer implements RepositoryRestConfigurer {


   @Override

   public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {

     config.exposeIdsFor(ThemeMessage.class);

   }

}

將以下行添加到您的轉換器(在 POST 請求期間,id 將為空):


@Override

public Serializable fromRequestId(String id, Class<?> aClass) {

    if(id==null) {

      return null;

    }

    String[] parts = id.split("_");

    return new EmployeeIdentity(parts[0], parts[1]);

}

然后以下POST請求將起作用:


{

  "id": {

       "employeeId": "E-267", 

       "companyId": "D-432"

  },

  "name": "Spider Man", 

  "email": "[email protected]", 

  "phoneNumber": "+91-476253455"

}

但是,id 字段將在所有響應中公開。但也許這不是一個真正的問題,因為當您使用復合 id 時,它通常意味著 id 不僅是一個抽象標識符,而且它的部分具有應該出現在實體主體中的有意義的內容。


實際上,我也在考慮將這些行添加到我自己的代碼中...... :)


查看完整回答
反對 回復 2022-06-30
?
青春有我

TA貢獻1784條經驗 獲得超8個贊

我有一個類似的問題,我找不到通過POST /entities端點創建新實體的解決方案。PUT /entities/{newId}但是,您也可以通過端點創建新實體。轉換器適用于這些端點。我還完全拒絕了 POST 端點,避免了 500 個響應:


  @PostMapping(value = "/themeMessages")

  public ResponseEntity<Void> postThemeMessage() {


    return new ResponseEntity<>(HttpStatus.METHOD_NOT_ALLOWED);

  }


查看完整回答
反對 回復 2022-06-30
  • 2 回答
  • 0 關注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號