Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Starter Data Rest change URL of repository from the root URI

Following the spring.io example here: http://spring.io/guides/gs/accessing-data-rest/ for exposing a repository as a rest web service works just fine, but I cannot see how to change the URL of the exposed service. The API documentation is a little vague as to what the annotation parameters mean, perhaps some prior knowledge is assumed.

What I want - A HATEOAS service accessed at http://localhost:8080/api/people for a People repository. I want to achieve this URL using annotations only, not messing with the context root or similar. I tried the following repository annotations:

  • @RepositoryRestResource(collectionResourceRel = "api/people", path = "people")
  • @RepositoryRestResource(collectionResourceRel = "people", path = "api/people")
  • @RepositoryRestResource(collectionResourceRel = "api/people", path = "api/people")

None of these work.

I know I have probably missed the obvious, much appreciate anyone who can point it out.

like image 419
mmeany Avatar asked Apr 29 '14 23:04

mmeany


People also ask

What does the @RepositoryRestResource annotation do?

The @RepositoryRestResource annotation is optional and is used to customize the REST endpoint. If we decided to omit it, Spring would automatically create an endpoint at “/websiteUsers” instead of “/users“. That's it! We now have a fully-functional REST API.

What is @RepositoryRestController?

Annotation Type RepositoryRestControllerAnnotation to demarcate Spring MVC controllers provided by Spring Data REST. Allows to easily detect them and exclude them from standard Spring MVC handling.

What is the use of spring boot starter Data rest?

Spring Data REST builds on top of Spring Data repositories, analyzes your application's domain model and exposes hypermedia-driven HTTP resources for aggregates contained in the model.


1 Answers

As of Spring Boot 1.2 you are able to set this property:

spring.data.rest.baseUri=api

Alternatively:

spring.data.rest.base-uri=api

(Spring Boot uses a relaxed binding system)

NOTE: I have found that if you have extended RepositoryRestMvcConfiguration with custom configuration, the property does not take effect. For more information see:

https://github.com/spring-projects/spring-boot/issues/2392

Once the next version of Spring Boot is released (after 1.2.1), the solution will be to extend RepositoryRestMvcBootConfiguration instead.

like image 188
JBCP Avatar answered Oct 08 '22 14:10

JBCP