I am developing a set of rest resources over a database and exposing the core CRUD functionality using Spring Data Rest to directly interact with the Repositories.
In my simplified sample I have Users:
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long id;
public String name;
@OneToMany(mappedBy = "user")
public Collection<Project> projects;
}
and users Own Projects:
@Entity
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long id;
public String name;
public String oneOfManyComplexDerivedProperties;
@ManyToOne
public User user;
}
Directly interacting with the repositories is fine, so for creating Users (other other simple entities), the problem comes with creating Projects. Projects have a large number of server derived fields based on the users form input, so I wrote a custom controller to generate them and persist the result. In order to persist the result, I need to associate the Project with it's owning User. I want my client to be able to use the user Link for this, just as when creating a new entity by going direct to the repository (going direct to the repository just works):
@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);
}
}
I get:
{
"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"
}
If I change userResource to type Resource
then I get a different error:
"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"
I can't find any reference to using repository URI's in custom controllers in the docs, the closest I found was Resolving entity URI in custom controller (Spring HATEOAS) but the API's have changed since that was written and I have not been able to get it to work.