我正在数据库上开发一组 REST 资源,并使用 Spring Data Rest 公开核心 CRUD 功能,以直接与存储库交互。
在我的简化示例中,我有用户:
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long id;
public String name;
@OneToMany(mappedBy = "user")
public Collection<Project> projects;
}
和用户自己的项目:
@Entity
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long id;
public String name;
public String oneOfManyComplexDerivedProperties;
@ManyToOne
public User user;
}
直接与存储库交互是可以的,因此对于创建用户(其他简单实体),问题在于创建项目。项目有大量基于用户表单输入的服务器派生字段,因此我编写了一个自定义 Controller 来生成它们并保留结果。 为了保留结果,我需要将项目与其所属的用户关联起来。我希望我的客户能够为此使用用户链接,就像通过直接访问存储库创建新实体时一样(直接访问存储库就可以了):
@RepositoryRestController
public class CustomProjectController {
@Autowired
ProjectRepo projectRepo;
@RequestMapping(value = "/createProject", method = RequestMethod.POST)
public HttpEntity<Project> createProject(@RequestParam User userResource,
@RequestParam String formField1, // actually an uploaded file that gets processed, but i want simple for example purposes
@RequestParam String formfield2)
{
Project project = new Project();
/*
Actually a large amount of complex business logic to derive properties from users form fields, some of these results are binary.
*/
String result = "result";
project.oneOfManyComplexDerivedProperties = result;
project.user = userResource;
projectRepo.save(project);
// aware that this is more complex than I've written.
return ResponseEntity.ok(project);
}
}
当我打电话时:
<a href="http://localhost:9999/api/createProject?userResource=http://localhost:9999/api/users/1&formField1=data1&formField2=Otherdata" rel="noreferrer noopener nofollow">http://localhost:9999/api/createProject?userResource=http://localhost:9999/api/users/1&formField1=data1&formField2=Otherdata</a>
我得到:
{
"timestamp": 1510588643801,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.method.annotation.MethodArgumentTypeMismatchException",
"message": "Failed to convert value of type 'java.lang.String' to required type 'com.badger.User'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'http://localhost:9999/api/users/1'; nested exception is java.lang.NumberFormatException: For input string: \"http://localhost:9999/api/users/1\"",
"path": "/api/createProject"
}
如果我将 userResource 更改为类型 Resource
然后我得到一个不同的错误:
"Failed to convert value of type 'java.lang.String' to required type 'org.springframework.hateoas.Resource'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.springframework.hateoas.Resource': no matching editors or conversion strategy found"
我在文档中找不到任何关于在自定义 Controller 中使用存储库 URI 的引用,我找到的最接近的是 Resolving entity URI in custom controller (Spring HATEOAS)但自编写以来 API 已经发生了变化,我无法让它工作。
最佳答案
我建议你真正应该做的是:
http://localhost:9999/api/users/1/projects?formField1=data1&formField2=Otherdata
通过启用 Spring Data 的 Web 支持,您可以将路径变量自动绑定(bind)到实体实例。
@RequestMapping(value = "users/{id}/projects", method = RequestMethod.POST)
public HttpEntity<Project> createProject(
@PathVariable("id") User user,
@RequestParam String formField1,
@RequestParam String formfield2)
{
}
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#core.web
关于java - 在带有 Spring REST JPA 的自定义 REST Controller 中使用链接资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47269121/