不明白 Spring(引导)数据 REST 如何被告知创建 API 端点

Don't understand how Spring (Boot) Data REST is told to create API endpoints

我目前有一个应用程序正在使用 Spring 引导和 Spring 数据。我的域对象是从我的数据库中逆向工程得到的,并且我有几个存储库 (类)。每个存储库都是一个扩展 CrudRepository 的接口。

import org.springframework.data.repository.CrudRepository

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

此时我将创建一个服务来实现这些项目。然后该服务将由 REST 控制器调用。

我希望能够 Spring 如果可能自动创建我的 REST API,我找到了 Spring Data REST 项目。我找到了这个 http://spring.io/guides/gs/accessing-data-rest/ 并且我可以按照该指南进行操作,但我不明白是什么使 "REST APIs" 能够被创建 "automatically"。如果 @RepositoryRestResource 注释导致 API 被创建,我可以理解,但在该指南中它明确表示

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.

包含在我的 POM 文件中并重建 "automatically" 是否允许 Spring 数据创建 REST 端点?

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

我缺少什么关键字、部分可以自动创建 API 端点?

Spring 引导自以为是。它有意见,例如使用 tomcat 作为您的应用程序服务器或使用 logback 作为您的日志记录实用程序。当你拉进来时

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

它有意见,当它检测到(通过类路径扫描)interfaces/classes extend/implement Repository 时,它会假设那些 类 应该作为 [=25] =] 资源。 RestRepository 允许您通过更改端点或根本不提供资源(exported = false)来自定义此行为。

Spring-Data-REST 会自动设置资源来处理 GET/POST/PUT/DELETE 请求。你熟悉 REST/HTTP 吗?这些不会是离散端点,GET/PUT/POST/DELETE 是 http verbs,因此不会有 resource/1/delete 端点。

Spring Data Rest 在包 org.springframework.data.rest.webmvc:

中实现了六个控制器

RepositoryController:处理 /

RepositoryEntityController:处理 /{repository}

RepositoryPropertyReferenceController:处理 /{repository}/{id}/{property}/{repository}/{id}/{property}/{propertyId}

RepositorySearchController:处理 /{repository}/search/{repository}/search/{repoFunctionName}

ProfileController:处理 /profile

RepositorySchemaController:处理 /profile/{repository}

它们本质上与 Spring MVC 控制器相同,只是它们被设计为以一般方式与 Spring 数据存储库一起工作

因此,如果您执行 GET /foo,其中 foofooRepositorypath,那么 Spring Data Rest 将调用 RepositoryEntityController.getCollectionResource(),并调用 fooRepository.findAll(...),将结果包装在一些 HATEOAS 对象中,然后使用 Jackson

编组到 JSON