Adding Spring Data REST to an Existing Spring MVC Application
如果你使用 Spring Boot,则以下步骤不必要。对于 Boot 应用程序,添加 |
The following steps are unnecessary if you use Spring Boot. For Boot applications, adding |
你可以将 Spring Data REST 与现有的 Spring MVC 应用程序集成。在你的 Spring MVC 配置中(很可能你配置 MVC 资源的地方),向负责配置 RepositoryRestController
的 Java 配置类添加一个 bean 引用。类名为 org.springframework.data.rest.webmvc.RepositoryRestMvcConfiguration
。以下示例演示如何使用 @Import
注解添加正确的引用:
You can integrate Spring Data REST with an existing Spring MVC application. In your Spring MVC configuration (most likely where you configure your MVC resources), add a bean reference to the Java configuration class that is responsible for configuring the RepositoryRestController
. The class name is org.springframework.data.rest.webmvc.RepositoryRestMvcConfiguration
. The following example shows how to use an @Import
annotation to add the proper reference:
该配置应如下所示:
The configuration would look like:
import org.springframework.context.annotation.Import;
import org.springframework.data.rest.webmvc.RepositoryRestMvcConfiguration;
@Configuration
@Import(RepositoryRestMvcConfiguration.class)
public class MyApplicationConfiguration {
…
}
<bean class="org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration"/>
当你的 ApplicationContext 遇到此 bean 定义时,它将引导必要的 Spring MVC 资源以完全配置控制器,用于导出它在该 ApplicationContext
和任何父上下文中找到的存储库。
When your ApplicationContext comes across this bean definition, it bootstraps the necessary Spring MVC resources to fully configure the controller for exporting the repositories it finds in that ApplicationContext
and any parent contexts.
More on Required Configuration
Spring Data REST 取决于几个 Spring MVC 资源,这些资源必须正确配置才能在现有 Spring MVC 应用程序中运行。我们尝试将这些资源与应用程序中已存在的任何类似资源隔离开,但你可能希望通过修改这些 MVC 组件来定制 Spring Data REST 的某些行为。
Spring Data REST depends on a couple Spring MVC resources that must be configured correctly for it to work inside an existing Spring MVC application. We tried to isolate those resources from whatever similar resources already exist within your application, but it may be that you want to customize some of the behavior of Spring Data REST by modifying these MVC components.
你应该特别注意配置下一部分介绍的 RepositoryRestHandlerMapping
。
You should pay special attention to configuring RepositoryRestHandlerMapping
, covered in the next section.
RepositoryRestHandlerMapping
我们注册了一个自定义的 HandlerMapping
实例,它只响应 RepositoryRestController
,并且仅当 Spring Data REST 应该处理某个路径时才响应。为了让应用程序应该处理的路径与 Spring Data REST 处理的路径分开,此自定义 HandlerMapping
类会检查 URL 路径,并查看是否已从此名称导出了存储库。已导出,则自定义 HandlerMapping
类允许 Spring Data REST 处理请求。如果未从此名称导出存储库,则它会返回 null
,这意味着“让其他 `HandlerMapping
实例尝试为该请求提供服务`”。
We register a custom HandlerMapping
instance that responds only to the RepositoryRestController
and only if a path is meant to be handled by Spring Data REST. In order to keep paths that are meant to be handled by your application separate from those handled by Spring Data REST, this custom HandlerMapping
class inspects the URL path and checks to see if a repository has been exported under that name. If it has, the custom HandlerMapping
class lets the request be handled by Spring Data REST. If there is no Repository exported under that name, it returns null
, which means “let other HandlerMapping
instances try to service this request”.
Spring Data REST HandlerMapping
配置为 order=(Ordered.LOWEST_PRECEDENCE - 100)
,这意味着在映射 URL 路径时,它通常排在第一位。你的现有应用程序永远没有机会为存储库提供的请求提供服务。例如,如果你有一个以 person
名称导出的存储库,那么你应用程序中所有以 /person
开头的请求都将由 Spring Data REST 处理,而你的应用程序永远看不到该请求。但是,如果你的存储库以其他名称(如 people
)导出,则对 /people
的请求将转至 Spring Data REST,而对 /person
的请求将由你的应用程序处理。
The Spring Data REST HandlerMapping
is configured with order=(Ordered.LOWEST_PRECEDENCE - 100)
, which means it is usually first in line when it comes time to map a URL path. Your existing application never gets a chance to service a request that is meant for a repository. For example, if you have a repository exported under the name of person
, then all requests to your application that start with /person
are handled by Spring Data REST, and your application never sees that request. If your repository is exported under a different name (such as people
), however, then requests to /people
go to Spring Data REST and requests to /person
are handled by your application.