Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I map a Spring Boot @RepositoryRestResource to a specific url?

I cannot seem to be able to map my Repository in any location other than the following:

@RepositoryRestResource(collectionResourceRel = "item", path = "item")
public interface ItemRepository extends PagingAndSortingRepository<Item, Long> {

I thought I can use:

 path = "/some/other/path/item"

but the mapping does not resolve. I get:

HTTP ERROR 404

Problem accessing /some/other/path/item. Reason:

Not Found

In spring-data javadoc path is defined as: "The path segment under which this resource is to be exported."

What am I doing wrong?

like image 658
stratosgear Avatar asked Jul 04 '14 15:07

stratosgear


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.

Why is Spring Data rest not recommended in real world applications?

It is not recommended in real-world applications as you are exposing your database entities directly as REST Services. While designing RESTful services, the two most important things that we consider are the domain model and the consumers. But, while using Spring Data REST, none of these parameters are considered.

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 collectionResourceRel?

and collectionResourceRel is described: The rel value to use when generating links to the collection resource.


2 Answers

To change the base URI, you can also just add this to application.properties:

spring.data.rest.base-path=/my/base/uri
like image 99
Bruce Edge Avatar answered Sep 21 '22 18:09

Bruce Edge


You need to extend the RepositoryRestMvcConfiguration and override the configureRepositoryRestConfiguration(RepositoryRestConfiguration config) to set yours baseUri. e.g.

@Configuration
public class MyRepositoryRestMvcConfiguration extends RepositoryRestMvcConfiguration {

    private static final String MY_BASE_URI_URI = "/my/base/uri";

    @Override
    protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        super.configureRepositoryRestConfiguration(config);
        config.setBaseUri(URI.create(MY_BASE_URI_URI));
    }
}
like image 45
gmich Avatar answered Sep 20 '22 18:09

gmich