【问题标题】:Loading Spring Data Rest and Spring Data JPA together一起加载 Spring Data Rest 和 Spring Data JPA
【发布时间】:2012-06-01 04:17:11
【问题描述】:

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

可以同时使用两个网络层吗?

可能是什么配置错误?

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

【问题讨论】:

  • 请添加更多详细信息,否则我们无法为您提供帮助。

标签: spring rest spring-data-jpa spring-data-rest


【解决方案1】:

我已经同时使用了这两种技术,没有任何问题。

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

【讨论】:

    【解决方案2】:

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

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

    糟糕的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>
    

    【讨论】:

      猜你喜欢
      • 2015-07-14
      • 1970-01-01
      • 2016-02-01
      • 2019-09-30
      • 2017-11-29
      • 2016-10-26
      • 2014-10-21
      • 2021-11-11
      • 1970-01-01
      相关资源
      最近更新 更多