New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ControllerLinkBuilder does not take Spring Data REST's base path into account #434
Comments
I just came across this issue as well. Did you find a workaround? |
Out of curiousity, is this a spring-hateoas problem or a spring-data-rest problem stemming from |
You should be using |
Maybe I'm confused about what this means:
I have a Am I not still interested in entity-specific operations in this case? |
Sorry. |
Have exactly the same problem. |
Same problem here. I also created a JIRA ticket: Intersting fact: When you create a controller with @RepositoryRestController (which extends BasePathAwareController), then the resource is added twice: Once under the root and once with basePath prefix. This can be seen in the spring logs. But only the version mapped under root actually does work. Smells like a bug for me ... |
Please don't open duplicate tickets. If you are moving to JIRA for data rest, then close this ticket. |
Sorry for the duplicate. I already opened the spring-data-rest JIRA ticket. before I found this here. I am actually not sure, if this is more a spring-hateoas or a spring-data-rest issue ... What do you think? |
I have the same problem and implemented the following service that prepends the base path as a workaround: @Service
public class BasePathAwareLinks {
private final URI contextBaseURI;
private final URI restBaseURI;
@Autowired
public BasePathAwareLinks(ServletContext servletContext, RepositoryRestConfiguration config) {
contextBaseURI = URI.create(servletContext.getContextPath());
restBaseURI = config.getBasePath();
}
public LinkBuilder underBasePath(ControllerLinkBuilder linkBuilder) {
return BaseUriLinkBuilder.create(contextBaseURI)
.slash(restBaseURI)
.slash(contextBaseURI.relativize(URI.create(linkBuilder.toUri().getPath())));
}
} To use it, just pass the public class MyResourceProcessor<Person> implements ResourceProcessor<Person> {
@Autowired
private BasePathAwareLinks service;
public Resource<Person> process(Resource<Person> resource) {
resource.add(
service.underBasePath(
linkTo(methodOn(SpecialPersonController.class).doSomething())
)
.withRel("something")
);
return resource;
}
} Hope this helps. |
Is there any progress on this issue? |
I have started poking at this issue. It appears, I can get the custom route to work when I use In the meantime, I reproduced the fact that |
I assume we will need a RestControllerLinkBuilder class in the data-rest package... |
We'd either need a link builder in SDR or the concept of a base path must move into Spring HATEOAS. I prefer the latter. |
As you probably might have expected, I have a slightly different view on this :). First of all, due to it's static nature, all a The reason we didn't do that already is that this naturally creates a mismatch between static and non-static usage of |
The work around code from @HiaslTiasl is useful, but does not work for templated links. |
spring-projects/spring-hateoas#434 Links built for @BasePathAwareControllers do not include the base path The workaround in the ticket does not work for templated links Therefore, manually add the base path to the controller code
@HiaslTiasl thanks for your code. It works but it looses the first part "http://ip:port. Is that supposed to happen? |
@drenda happy to help! 😺 For me the first part was not important, but in general we probably should consider it as well. I guess you could extract it from public LinkBuilder underBasePath(ControllerLinkBuilder linkBuilder) {
URI uri = linkBuilder.toUri();
URI origin = new URI(uri.getScheme(), uri.getAuthority(), null, null, null);
URI suffix = new URI(null, null, uri.getPath(), uri.getQuery(), uri.getFragment());
return BaseUriLinkBuilder.create(origin)
.slash(contextBaseURI)
.slash(restBaseURI)
.slash(suffix);
} We basically take the link from the |
@HiaslTiasl Thanks it works perfectly! |
Here is my workaround:
Then I have a method which inserts
And now I just wrap all Example: |
Hey, I was fighting with this issue myself and found out that you can add base path of SDR to @RequestMapping annotation explicitly and here is what happens: you get proper link using static ControllerLinkBuilder AND your method gets registered properly as you want it - the prefix with base path will be ignored somehow. Although I'm not sure how it is happening, this is what I'm using and it works fine for me. |
Simple workaround, based on Michael Tran solution:
Usage is the same as linkTo:
|
Other workaround for simple case based on current request with addPath:
|
this code work fine for me :
|
Faced the same problem, You can now use RepositoryEntityLinks
|
I recently faced the same problem and tried to solve the issue with the above mentioned fixes. Unfortunately all of them seem to have the same problem: My solution would look like this:
It would be used like this:
|
Hi, There was a bug fix in the Spring HATEOAS library or an official workaround for this? Thanks you. |
I anybody needs a solution for this, here is mine: a LinkBuilder implementation based on WebMvcLinkBuilder that handles the base-path and the BasePathAwareController Annotation. A few details:
|
I have the repository REST base path set to
/rest
, and a@RepositoryRestController
with some custom handlers:The endpoint works fine at
http://localhost:8080/rest/foo
, however, the link is incorrect:For some reason, the base path (
/rest
) was ignored.I'm using Spring HATEOAS 0.19.0.RELEASE (comes with Spring Boot 1.3.1.RELEASE).
The text was updated successfully, but these errors were encountered: