spring-projects / spring-projects/spring-hateoas

How to fech JPA entity from HATEOAS URI (self.href)

Open
#774 8 comments 1 reaction 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Mar 5, 2019.

Dominant language
Java
Stars
1.1k
Forks
476
PR merge metrics
No merged PRs in 30d

Description

I really like the HATEOAS concept that URIs are used as IDs. For example when I have an POJO Entity Comment it can simply be referenced as /myBasePath/comments/4711

spring-hateoas has great support for building those URIs when I have an JPA entity:

Link entityUriLink = entityLinks.linkToSingleResource(Comment.class, comment.getId());

BUT there is no way for the other way round. There no (easy) way to get the JPA entity when you just have its URI.

Why and when is this necessary? In a custom rest controller, an enttiy URI might be passed as a @RequestParam. Spring data rest actually IS able to fetch the entity from its plain (internal, numerical) ID. This works:

@BasePathAwareController  
public class MyCustomController {
  @RequestMapping("/customEndpoint")
  public ResponseEntity customEndpoint(@RequestParam("commentId") CommentModel comment) { ... }

}

GET /myBasePath/customEndpoint?commentId=4711   <= works
GET /myBasePath/customEndpoint?commentId=/comments/4711   <= DOES NOT WORK
GET /myBasePath/customEndpoint?commentId=/myBasePath/comments/4711   <= DOES NOT WORK
GET /myBasePath/customEndpoint?commentId=http://localhost:8080/myBasePath/comments/4711   <= DOES NOT WORK

There would be org.springframework.data.rest.core.UriToEntityConverter but this class is very difficult to create, because it needs so many dependencies (PersistentEntities, RepositoryInvokerFactory, Repositories)

I have a workaround, but an ugly one. You can configure your own conversionService in a RepositoryRestConfigurer. But then its an ugly parsing of IDs from String in there.

=> How to get the JPA entity when I only have the URI of it?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.