我已经同时使用了这两种技术,没有任何问题。
我有一个现有的 Spring MVC 应用程序,其中包含使用 Spring 数据 JPA 创建的存储库。然后我在顶部添加了 Spring 数据 REST。
Servlet 3.0 配置
// Register and map the standard dispatcher servlet
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(mvcContext));
dispatcher.setInitParameter("contextConfigLocation", "/WEB-INF/spring/appServlet/servlet-context.xml");
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/app/*");
// Register the Spring data rest exporter servlet
ServletRegistration.Dynamic exporter = servletContext.addServlet("exporter", RepositoryRestExporterServlet.class);
exporter.addMapping("/rest/*");
Maven pom.xml 配置
<!--
NB. This pulls in Spring data JPA
and just about everything else you need.
-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-webmvc</artifactId>
<version>1.0.0.RC2</version>
</dependency>
您需要在META-INF/spring-data-rest 中添加一个xxx-export.xml 文件。我只是将其指向我现有的根配置上下文。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="../../root-context.xml"/>
</beans>
最后,我的根配置
<context:component-scan base-package="my.package.spring"/>
<jpa:repositories base-package="my.package.spring.repo"/>
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="entityManagerFactory" ...>
...
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="dataSource" ...>
...
</bean>
我现有的@Controllers 仍然在localhost:8080/myapp/app/* 可用,新的休息端点导出到localhost:8080/myapp/rest/*。