Riccardo
Riccardo

Reputation: 11

Loading Spring Data Rest and Spring Data JPA together

I’m trying Spring-Data-JPA and Spring-Data-Rest together in the same web application, but they don’t work correctly. The application has all maven dependencies needed and they are up-to-date.

It’s possible to use the two web tiers at the same time?

What error in configuration could be?

Has someone any suggestion to set them correctly?

Upvotes: 0

Views: 1385

Answers (2)

lopisan
lopisan

Reputation: 7830

I've just struggled with the same problem - when I added spring-data-rest-webmvc to maven pom.xml, Spring data JPA stopped working (problem with loading of Spring applicationContext.xml - unknown attribute).

The problem was in mismatch of spring-data-jpa versions - spring-data-rest-webmvc has it's own dependency on spring-data-jpa, so I had to remove my spring-data-jpa from pom.xml:

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

Fixed:

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

Upvotes: 1

Rich Cowin
Rich Cowin

Reputation: 678

I've used the 2 technologies side-by-side without issue.

I have an existing Spring MVC application with repositories created using Spring data JPA. I then added Spring data REST on top.

Servlet 3.0 config

    // 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 config

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

You need to add in a xxx-export.xml file in META-INF/spring-data-rest. I just point this to my existing root configuration context.

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

finally, my root config

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

My existing @Controllers are still available at localhost:8080/myapp/app/* and the new rest endpoints are exported to localhost:8080/myapp/rest/*.

Upvotes: 2

Related Questions