question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
chryliscommented, Mar 5, 2019

Greg, this has been a concern that I brought up with version 0.11 or so. Without the ability to do round-trip mapping, the entire infrastructure is essentially useless because link handling devolves into a bunch of hand-parsing.

1reaction
gregturncommented, May 2, 2019

@Doogiemuc in SDR, you have RepositoryEntityLinks that lets you turn domain objects into links. That’s because SDR defines all the routes and can thus leverage metadata. And as you’ve noted, SDR also has UriToEntityConverter, which lets you go the other direction.

But with Spring HATEOAS, the only place where routes are defined are in the annotations in your own custom controllers. Spring MVC/WebFlux (the underlying web stack) has no centralized route table that this data is gathered into, hence no way to do a reverse lookup.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring HATEOAS - Reference Documentation
Spring HATEOAS lets you work with links through its immutable Link value type. Its constructor takes both a hypertext reference and a link...
Read more >
Applying HATEOAS to a REST API with Spring Boot
In this scenario it returns ResponseEntity<PersonResource> (HATEOAS) instead of ResponseEntity<Person> (REST). So what is this resource object I keep mentioning ...
Read more >
Spring get link to an entity - Stack Overflow
I solved it by adding this to my class: @Autowired EntityLinks entityLinks;. and using some of the HATEOAS features of Spring. Link link...
Read more >
Spring Boot REST API CRUD with HATEOAS Tutorial
Tutorial for developing REST APIs that follow HATEOAS principle with Spring Boot and Spring HATEOAS. Adding hypermedia links for REST APIs.
Read more >
Working with Relationships in Spring Data REST - Baeldung
In this tutorial, we'll learn how to work with relationships between entities in Spring Data REST. We'll focus on the association resources ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found