Using RepositoryRestResource annotation to change RESTful endpoint not working Using RepositoryRestResource annotation to change RESTful endpoint not working mongodb mongodb

Using RepositoryRestResource annotation to change RESTful endpoint not working


You can't use slash inside the path attribute, but you can set base path in application.properties:

# DATA REST (RepositoryRestProperties)spring.data.rest.base-path=/my/base/uri# Base path to be used by Spring Data REST to expose repository resources.


Without seeing your entire configuration it is hard to know exactly what is going on in your situation. However using the latest guide at https://github.com/spring-guides/gs-accessing-data-mongodb.git I am able to get it working by making the following changes:

  • Adding spring-boot-starter-data-rest as a dependency in the POM file.

    <dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-data-rest</artifactId></dependency>
  • Adding this annotation to the CustomerRepository class.

    @RepositoryRestResource(path = "people")
  • Setting up getters and setters in the Customer class for the 2 name fields in the constructor to avoid a Jackson serialization error.

Using this when I run the application I am able to access the repository at http://localhost:8080/people. If I remove the annotation then the CustomerRepository is accessed at http://localhost:8080/customers. Let me know if you want me to post a fork on GitHub.

To answer your question about what RepositoryRestResource is that it overrides the attributes for the ResourceMapping that is created by default. It's attributes are used in creating the mapping and change the related return values of the methods on the mapping class. By default Spring Data Rest creates defaults based on the class names of the objects used in the repository definition.


/customerModels is generated by default because your default method returns a list of CustomerModel. So you may try to add this @RestResource(path = "names") to your method and then access it like:http://localhost:8080/yourapp/people/search/names. Look here: Spring data docs