spring-projects / spring-data-rest

Simplifies building hypermedia-driven REST web services on top of Spring Data repositories
https://spring.io/projects/spring-data-rest
Apache License 2.0
916 stars 562 forks source link

Custom Controller + changed base path doesn't show up in HAL [DATAREST-748] #1120

Open spring-projects-issues opened 8 years ago

spring-projects-issues commented 8 years ago

George Hilios opened DATAREST-748 and commented

Hi, I've created a custom controller to access a custom repository. I use ControllerLinkBuilder to add a link to the resource in HAL when accessing the root, and also change the base path to "/api". I cannot get a correct link (under /api) to show up in HAL for the custom controller.

What I've tried: 1) Using @RequestMapping("/samples"). HAL link is correct (http://localhost/api/samples), but it also serves the request on http://localhost/samples because of the RequstMappingHandler. I need everything outside of /api to go to React, so that won't work for me 2) Using @BaseAwareController. The self-contained repro has this. It works only if @RequestMapping is also set. 3) Leaving off @RequestMapping on the type, but including it on the methods. The /api endpoints work perfectly, but HAL only shows http://localhost

I've included a completely self-contained repro with unit tests to show what I expect: https://github.com/ghilios/SDR-HATEOAS-issue


Reference URL: https://github.com/ghilios/SDR-HATEOAS-issue

3 votes, 6 watchers

spring-projects-issues commented 8 years ago

Oliver Drotbohm commented

Thanks, George. I'll have a look

spring-projects-issues commented 8 years ago

Lukas Fischer commented

Any updates on this? I came across the same issue. My findings to the points stated above, please correct me if something is wrong:

1) & 2) @RequestMapping shouldn't be used on class/type level for @RepositoryRestController & @BasePathAwareController. It will create two endpoints like you encountered. 2) That should be the correct way. However, ControllerLinkBuilder doesn't take the base path in account. It only checks the @RequestMapping annotations. It should rather do something like checking if @BasePathAwareController is annotated and then prepend the value of spring.data.rest.basePath

spring-projects-issues commented 8 years ago

Michael Tran commented

I also have the same issue. I am trying to override the defaults resources from the Hal Browser. My workaround is to make a RestController with path /api and add the links manually using ResourceProcessor\.

Or if you want to keep as RepositoryRestController. Load the base-path from your application properties.

UriComponentsBuilder uriComponentsBuilder = linkTo(methodOn(EventController.class).getEvents()).toUriComponentsBuilder();
URL url = new URL(uriComponentsBuilder.toUriString());
uriComponentsBuilder.replacePath(basePath + url.getPath());
Link link = new Link(uriComponentsBuilder.toString());