'How to add links to root resource in Spring Data REST?
How to expose an external resource (not managed through a repository) in the root listing of resources of Spring Data REST? I defined a controller following the pattern in Restbucks
Solution 1:[1]
I have been searching for an answer to the same issue, but the key is: I don't have a controller. My url points to something created in an auth filter. What worked for me is to create a RootController that doesn't have any methods, and use it for building links in the ResourceProcessor implementation.
@RestController
@RequestMapping("/")
public class RootController {}
Then the link is inserted using the empty controller.
@Component
public class AuthLinkProcessor implements ResourceProcessor<RepositoryLinksResource> {
@Override
public RepositoryLinksResource process(RepositoryLinksResource resource) {
resource.add(
linkTo(RootController.class)
.slash("auth/login")
.withRel("auth-login"));
return resource;
}
}
Solution 2:[2]
In 2022, API has changed. This reply might be relevant: Migrating ResourceProcessor to HATEOAS 1.0.0 M1.
Here's my piece of code with the new API:
@Component
class AuthLinkProcessor implements RepresentationModelProcessor<RepositoryLinksResource> {
@Override
public RepositoryLinksResource process(RepositoryLinksResource model) {
model.add(
linkTo(AuthenticationController.class)
.slash("/authenticate")
.withRel("authenticate"));
return model;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ld00d |
| Solution 2 | hsrv |
