9

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.

mmeany
  • 1,123
  • 10
  • 11
  • possible duplicate of [Spring Data Rest base path](http://stackoverflow.com/questions/22024716/spring-data-rest-base-path) – JBCP Jan 22 '15 at 04:08
  • in your application.properties: spring.data.rest.base-path=/api source: http://docs.spring.io/spring-data/rest/docs/current/reference/html/#_changing_the_base_uri – CodeFactory.DEV Mar 25 '16 at 06:15

3 Answers3

9

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.

JBCP
  • 13,109
  • 9
  • 73
  • 111
  • Is there any real advantage to setting this in a property vs in your class that extends RepositoryRestMvcConfiguration ? – John Dahle Jan 11 '15 at 22:19
  • 2
    It's a bit cleaner and Springy since you probably have an applications property file anyway. Doing it in code is more typing and more code that should be tested. Relying on the spring convention means you don't need to test your own code or have a different property. Lastly, it makes on boarding new developers easier, since it's a standard convention instead of custom code. For this property itself it's probably not a big deal, but it's the principle of it – JBCP Jan 11 '15 at 23:04
  • http://docs.spring.io/spring-boot/docs/1.2.x/reference/html/common-application-properties.html It's spring.data.rest.base-uri – acsadam0404 Feb 20 '15 at 11:27
  • @adam0404 - either will work according to Spring Boot's property loader. – JBCP Feb 20 '15 at 18:13
0

As of Spring Boot 1.4.3 the code should be :

spring.data.rest.base-path:api

(I think baseUri is deprecated since 1.2.3)

Ozzy
  • 61
  • 1
  • 5
  • Welcome to SO. Please read this [how-to-answer](http://stackoverflow.com/help/how-to-answer) in case you haven't read it. – thewaywewere May 06 '17 at 19:52
0

Although I couldn't change the base path of the REST services using the annotation @RepositoryRestResource combined with a CrudRepository, I managed to do it using a JpaRepository and a custom controller with the annotation @RequestMapping.

The repository could be something like:

@Repository
interface PersonRepository : JpaRepository<Person, Long>

And the controller:

@RestController
@RequestMapping("/api/people")
class PersonRestController(private val personRepository: PersonRepository) {
    ...

On the other hand, you can change the base path of all your REST services modifying it in the application.properties file of your project. Add the lines:

# DATA REST (RepositoryRestConfiguration)
spring.data.rest.base-path = api

Change api with the path you wish you use in your URLs. The first line is a comment and, as so, it's not mandatory, but is useful to mark the nature of the configuration value for future references.

You can find all the common application properties of Spring Boot 2.0.1 in the Appendix A of the documentation.

enreas
  • 11,152
  • 3
  • 31
  • 32