Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在spring引导数据rest的POST json中传递@EmbeddedId_Java_Spring Boot_Spring Data Rest - Fatal编程技术网

Java 如何在spring引导数据rest的POST json中传递@EmbeddedId

Java 如何在spring引导数据rest的POST json中传递@EmbeddedId,java,spring-boot,spring-data-rest,Java,Spring Boot,Spring Data Rest,我使用SpringBootDataREST构建了一个RESTAPI。我使用了一个embeddedId,还实现了一个BackendIdConverter 下面是我的可嵌入类 @Embeddable public class EmployeeIdentity implements Serializable { @NotNull @Size(max = 20) private String employeeId; @NotNull @Size(max = 20

我使用SpringBootDataREST构建了一个RESTAPI。我使用了一个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;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
}
以及使用嵌入id而不是限定类名正确生成资源链接

@Component
public class EmployeeIdentityIdConverter implements BackendIdConverter {

    @Override
    public Serializable fromRequestId(String id, Class<?> aClass) {
        String[] parts = id.split("_");
        return new EmployeeIdentity(parts[0], parts[1]);
    }

    @Override
    public String toRequestId(Serializable source, Class<?> aClass) {
        EmployeeIdentity id = (EmployeeIdentity) source;
        return String.format("%s_%s", id.getEmployeeId(), id.getCompanyId());
    }

    @Override
    public boolean supports(Class<?> type) {
        return Employee.class.equals(type);
    }
}
这不管用。EmployeeIdentityIdConverter#fromRequestId引发空指针异常,因为字符串参数为空。所以我添加了一个空检查,并在id为空时返回默认的EmployeeIdentity。正如这个答案所描述的

从请求ID修改的EmployeeIdentityIdConverter

@Override
public Serializable fromRequestId(String id, Class<?> aClass) {
    if (id == null) {
        return new EmployeeIdentity();
    }
    String[] parts = id.split("_");
    return new EmployeeIdentity(parts[0], parts[1]);
}
注意

我甚至不知道我在上面做了什么。我只是想在小问题发生时解决它们

顺便说一句,如果你猜这不起作用,那么你是对的。虽然我没有收到错误并且请求成功,但我没有得到预期的行为。创建了一个新条目,其中employeeId和companyId为空


如何制作模型使用@EmbeddedId和spring引导数据REST的POST-to-REST API?

我遇到了类似的问题,我无法找到通过
POST/entities
端点创建新实体的解决方案。 但是,您也可以通过
PUT/entities/{newId}
端点创建新实体。转换器可以很好地用于这些端点。 我还完全否认了POST端点避免500个响应:

  @PostMapping(value = "/themeMessages")
  public ResponseEntity<Void> postThemeMessage() {

    return new ResponseEntity<>(HttpStatus.METHOD_NOT_ALLOWED);
  }
@PostMapping(value=“/themmessages”)
公共响应主题后消息(){
返回新的响应属性(HttpStatus.METHOD\u不允许);
}

这里是另一个解决方案。(但仍不完美。)

公开Employee类的id:

@Configuration
  protected class MyRepositoryRestConfigurer implements RepositoryRestConfigurer {

   @Override
   public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
     config.exposeIdsFor(ThemeMessage.class);
   }
}
将以下行添加到转换器(POST请求期间,id将为空):

但是,id字段将在所有响应中公开。但这可能不是一个真正的问题,因为当您使用复合id时,它通常意味着该id不仅是一个抽象标识符,而且它的各个部分都有应该出现在实体体中的有意义的内容


实际上,我也在考虑将这些行添加到我自己的代码中……)

还有一件事!使用
~
作为分隔符。下划线很容易成为id字符串中的有效字符。是否使用了spring数据rest存储库?这个postThemeMessage()适合哪里?我有一个
ThemeMessage
实体的存储库,还有一个DataRest端点:
/themeMessages
。如果您使用与现有DataRest存储库端点匹配的URL定义控制器方法,则您的方法具有优先级,因此您可以覆盖默认功能。谢谢@Selindek。我用了这个版本。ID是公开的,但正如您所说,我怀疑这是个大问题,因为我们使用的是复合ID。但由于某些原因,我无法使用POST/employees/E-267_D-432更新条目。即使我正确输入了ID,我也会收到404错误。有什么想法吗?我更愿意在不添加控制器的情况下实现这一点。我只想使用spring数据存储库
public Employee() {
    this.employeeId = "";
    this.companyId = "";
}
  @PostMapping(value = "/themeMessages")
  public ResponseEntity<Void> postThemeMessage() {

    return new ResponseEntity<>(HttpStatus.METHOD_NOT_ALLOWED);
  }
@Configuration
  protected class MyRepositoryRestConfigurer implements RepositoryRestConfigurer {

   @Override
   public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
     config.exposeIdsFor(ThemeMessage.class);
   }
}
@Override
public Serializable fromRequestId(String id, Class<?> aClass) {
    if(id==null) {
      return null;
    }
    String[] parts = id.split("_");
    return new EmployeeIdentity(parts[0], parts[1]);
}
{
  "id": {
       "employeeId": "E-267", 
       "companyId": "D-432"
  },
  "name": "Spider Man", 
  "email": "spman@somedomain.com", 
  "phoneNumber": "+91-476253455"
}