如何在spring-boot数据休息的POST json中传递@EmbeddedId

我已经使用 Spring Boot Data REST 构建了一个 REST API。我正在使用 EmbeddedId,并且还实现了 BackendIdConverter。


下面是我的可嵌入类


@Embeddable

public 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;

    }


烙印99
浏览 104回答 2
2回答

慕仙森

这是另一个解决方案。(虽然仍然不完美。)公开 Employee 类的 id:@Configuration&nbsp; protected class MyRepositoryRestConfigurer implements RepositoryRestConfigurer {&nbsp; &nbsp;@Override&nbsp; &nbsp;public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {&nbsp; &nbsp; &nbsp;config.exposeIdsFor(ThemeMessage.class);&nbsp; &nbsp;}}将以下行添加到您的转换器(在 POST 请求期间,id 将为空):@Overridepublic Serializable fromRequestId(String id, Class<?> aClass) {&nbsp; &nbsp; if(id==null) {&nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }&nbsp; &nbsp; String[] parts = id.split("_");&nbsp; &nbsp; return new EmployeeIdentity(parts[0], parts[1]);}然后以下POST请求将起作用:{&nbsp; "id": {&nbsp; &nbsp; &nbsp; &nbsp;"employeeId": "E-267",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;"companyId": "D-432"&nbsp; },&nbsp; "name": "Spider Man",&nbsp;&nbsp; "email": "spman@somedomain.com",&nbsp;&nbsp; "phoneNumber": "+91-476253455"}但是,id 字段将在所有响应中公开。但也许这不是一个真正的问题,因为当您使用复合 id 时,它通常意味着 id 不仅是一个抽象标识符,而且它的部分具有应该出现在实体主体中的有意义的内容。实际上,我也在考虑将这些行添加到我自己的代码中...... :)

青春有我

我有一个类似的问题,我找不到通过POST /entities端点创建新实体的解决方案。PUT /entities/{newId}但是,您也可以通过端点创建新实体。转换器适用于这些端点。我还完全拒绝了 POST 端点,避免了 500 个响应:&nbsp; @PostMapping(value = "/themeMessages")&nbsp; public ResponseEntity<Void> postThemeMessage() {&nbsp; &nbsp; return new ResponseEntity<>(HttpStatus.METHOD_NOT_ALLOWED);&nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java