In my project I have two domain models. A parent and a child entity. The parent references a list of child entitires. (e.g. Post and Comments) Both entities have their spring data JPA CrudRepository<Long, ModelClass>
interfaces which are exposed as @RepositoryRestResource
HTTP GET and PUT operations work fine and return nice HATEOS representation of these models.
Now I need a special REST endpoint "create a new Parent that references one ore more already existing child entities". I'd like to POST the references to the children as a text/uri-list that I pass in the body of the request like this:
POST http://localhost:8080/api/v1/createNewParent
HEADER
Content-Type: text/uri-list
HTTP REQUEST BODY:
http://localhost:8080/api/v1/Child/4711
http://localhost:8080/api/v1/Child/4712
http://localhost:8080/api/v1/Child/4713
How do I implement this rest endpoint? This is what I tried so far:
@Autowired
ParentRepo parentRepo // Spring Data JPA repository for "parent" entity
@RequestMapping(value = "/createNewParent", method = RequestMethod.POST)
public @ResponseBody String createNewParentWithChildren(
@RequestBody Resources<ChildModel> childList,
PersistentEntityResourceAssembler resourceAssembler
)
{
Collection<ChildModel> childrenObjects = childList.getContent()
// Ok, this gives me the URIs I've posted
List<Link> links = proposalResource.getLinks();
// But now how to convert these URIs to domain objects???
List<ChildModel> listOfChildren = ... ???? ...
ParentModel newParnet = new ParentModel(listOfChildren)
parentRepo.save(newParent)
}
Reference / Related https://github.com/spring-projects/spring-hateoas/issues/292