I am trying to create a link to a RepositoryRestController from a normal RestController. The links being created by WebMvcLinkBuilder do not contain the expected spring.data.rest.basePath. RepositoryEntityLinks doesn't exactly work because I'm overriding the default controller.
I'm currently using Spring Boot version (2.6.14).
I've tried using the linkTo and methodOn functions to create the link, but this fails to add the desired base path to the link. I've referred to the documentation for Spring Hateoas WebMvcLinkBuilder for these functions but haven't been able to get any variations of them to work. When I try to use these I get the desired link except the base path is not present in the link.
Below is a simplified example of what I'm attempting to do.
Controller:
@BasePathAwareController
public class BookController {
@GetMapping(path = "/books/metadata")
public ResponseEntity<RepresentationModel<?>> getBooksMetadata()
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException {
RepresentationModel<?> representationModel = new RepresentationModel<>();
representationModel.add(Link
.of(linkTo(methodOn(BookEntityController.class).getBooks(null, null))
.withSelfRel().getHref())
.withRel(LinkRel.LIST));
return ResponseEntity.ok(representationModel);
}
EntityController:
@RepositoryRestController
public class BookEntityController {
@GetMapping(path = "/books")
public PagedModel<EntityModel<BookDTO>> getBooks(
// code that returns books
}
}
https://github.com/spring-projects/spring-hateoas/issues/434