0
votes

I have currently have an application that is using Spring Boot and Spring Data. I have my domain objects that were reverse engineered from my database and I have several repositories (classes). Each repository is an interface that extends the CrudRepository.

import org.springframework.data.repository.CrudRepository

interface MyDomainClassRepository extends CrudRepository<MyDomainClass, Integer> {
     private MyDomainClass findByName(String name);
}

At this point I would create a service that would implement these items. The service would then be called by a REST controller.

I wanted to be able to have Spring create my REST API automatically if possible and I found the Spring Data REST project. I found this http://spring.io/guides/gs/accessing-data-rest/ and I can follow that guide, but I don't understand what is enabling the "REST APIs" to be created "automatically". I could understand it if the @RepositoryRestResource annotation caused the API to be created but in that guide it explicitly says

RepositoryRestResource is not required for a repository to be exported. It is only used to change the export details, such as using /people instead of the default value of /persons.

Does including in my POM file and rebuilding "automatically" allow Spring Data to create the REST endpoints?

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>

What keyword, section am I missing that makes it so the API endpoint is created automatically?

1
I'm not sure what you mean by endpoint in your context but if you'd like to have an endpoint for viewing, say, users you would have to create a RequestMapping for it in your controller. There is also a Spring Actuator service that has Spring defined endpoints. See this. - px06
@px06 I was able to do that with @RequestMapping in my controller. But I don't see what the difference in doing it through @RequestMapping or @RepositoryRestResource(collectionResourceRel = "people", path = "people")? For example where did "localhost:8080/people/search" come from? That seems to be automatically generated. I was under the impression that the Spring Data REST would automatically create my DELETE, POST for me instead of me having to do it. My problem is, based on the example, I don't see how it is being told to do that. - Kevin Vasko

1 Answers

3
votes

Spring Boot is opinionated. It has opinions like using tomcat as your application server or logback as your logging utility. When you pull in

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>

It has opinions that when it detects (by classpath scanning) interfaces/classes that extend/implement Repository it will assume that those classes should be served as RESTful resources. RestRepository allows you to customize this behavior by changing the endpoint or not serving the resource at all (exported = false).

Spring-Data-REST does automatically setup the resource to handle GET/POST/PUT/DELETE requests. Are you familiar with REST/HTTP? Those would not be discrete endpoints, GET/PUT/POST/DELETE are http verbs, so there wouldn't be a resource/1/delete endpoint.