Tuesday, November 20, 2012

Spring MVC 3.2 with Spring Data Rest (Part 1)

In this tutorial, we will update an existing Spring MVC app to Spring MVC 3.2 and add RESTful endpoints using Spring Data Rest. The goal is to demonstrate how we can implement HATEOAS methodology using Spring.

Before we start, please take some time to review our existing application: Spring MVC 3.1, jqGrid, and Spring Data JPA Integration Guide. If all you need is a pure Spring Data Rest application, please visit the official Spring Data Rest starter web application.

Table of Contents

Part 1: Configuration
  • Update the pom.xml
  • Update the web.xml
  • Spring Configurations
  • Repositories
Part 2: Running the application

Dependencies

  • Spring core 3.2.0.RC1
  • Spring Data Rest 1.0.0.RC3
  • Spring Data JPA 1.1.0.RELEASE
  • jQuery 1.6.4
  • jqGrid 4.3.1
  • See pom.xml for details

Github

To access the source code, please visit the project's Github repository (click here)

Update the pom.xml

Here are the changes that we need to do:
  • Update the Spring core version
  • Update the Spring Data JPA version
  • Add Spring Data Rest dependency



Note: I also added the profiles section at the end of the pom.xml, so that we can expose the repositories in a clean manner.

Update the web.xml

Here are the changes that we need to do:
  • Update the web-app version to 2.5 (optional)
  • Update the display-name (optional)
  • Add Spring Data Rest servlet



Spring Configurations

We need to do three tasks:
  • Update the database name
  • Create a spring-data-rest.xml
  • Update the applicationContext.xml

Update the database name

Open the spring.properties under WEB-INF folder, and update it as follows:


In the original application, the declared database name is spring_jqgrid_tutorial, let's update it to spring_data_rest_tutorial (though this is really not needed).

Create a spring-data-rest.xml



Update the applicationContext.xml

There's not much update here. We just need to import the spring-data-rest.xml as follows:


Repositories

We need to do two tasks:
  • Update UserRepository
  • Create a new repository RoleRepository

The only update here is the addition of the annotation @Param to the UserRepository. This is required so that we can expose the parameters in the search queries.


We need to create a new repository for the Role domain so that we can expose it as RESTful endpoint:


Next

In the next section, we will build and run the application and test our RESTful endpoints. Click here to proceed.
StumpleUpon DiggIt! Del.icio.us Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google I'm reading: Spring MVC 3.2 with Spring Data Rest (Part 1) ~ Twitter FaceBook

Subscribe by reader Subscribe by email Share

8 comments:

  1. This is perfect, jsut what I looking for, just one update, for spring-data-rest-webmvc version: 1.1.0.M1I had to use

    org.springframework.data.rest.webmvc.RepositoryRestDispatcherServlet

    instead of

    org.springframework.data.rest.webmvc.RepositoryRestExporterServlet

    ReplyDelete
  2. Also comment out this in spring-data-rest.xml , since "RepositoryRestConfiguration" has been removed in 1.1.0.M1 version as well.

    ReplyDelete
  3. Is anyone else getting "Dependency not found" for version 1.1.0.M1?

    ReplyDelete
  4. Yes, me too.
    Still no idea yet, how to fix it.

    ReplyDelete
  5. Added the following maven repositories in pom.xml for getting the version 1.1.0.M1:

    id: com.springsource.repository.bundles.release
    url: http://repository.springsource.com/maven/bundles/release

    id: com.springsource.repository.bundles.external
    url: http://repository.springsource.com/maven/bundles/external

    id: com.springsource.repository.libs-snapshot
    url: http://repo.springsource.org/libs-snapshot

    Regards,

    Matthias

    ReplyDelete
  6. How to get the primary key value like ("ID")

    ReplyDelete
  7. I have read your blog its very attractive and impressive. I like it your blog.

    Spring online training Spring online training Spring Hibernate online training Spring Hibernate online training Java online training

    spring training in chennai spring hibernate training in chennai

    ReplyDelete