0

我在同一个 Web 应用程序中同时尝试 Spring-Data-JPA 和 Spring-Data-Rest,但它们不能正常工作。该应用程序具有所需的所有 Maven 依赖项,并且它们是最新的。

可以同时使用两个 Web 层吗?

可能是什么配置错误?

有人建议正确设置它们吗?

4

2 回答 2

2

我已经并排使用了这两种技术,没有问题。

我有一个现有的 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>

您需要xxx-export.xmlMETA-INF/spring-data-rest. 我只是将其指向我现有的根配置上下文。

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

我现有@Controller的 s 仍然可用,localhost:8080/myapp/app/*新的 rest 端点导出到localhost:8080/myapp/rest/*.

于 2012-09-13T15:56:40.713 回答
1

我刚刚遇到同样的问题 - 当我添加spring-data-rest-webmvc到 maven时pom.xml,Spring data JPA 停止工作(加载 Spring 的问题applicationContext.xml- 未知属性)。

问题在于spring-data-jpa版本不匹配 -spring-data-rest-webmvc它有自己的依赖关系spring-data-jpa,所以我不得不删除我spring-data-jpapom.xml

pom.xml

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>1.4.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-rest-core</artifactId>
    <version>1.0.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-rest-webmvc</artifactId>
    <version>1.0.0.RELEASE</version>
</dependency>

固定的:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-rest-core</artifactId>
    <version>1.0.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-rest-webmvc</artifactId>
    <version>1.0.0.RELEASE</version>
</dependency>
于 2013-11-26T08:51:39.950 回答