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

10 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. Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place.. Wondering where to go in 2019? Things to do has ranked as the best include a remote, idyllic island, the design capital ...

    ReplyDelete
  8. Effective blog with a lot of information. I just Shared you the link below for Courses .They really provide good level of training and Placement,I just Had Spring Classes in this institute , Just Check This Link You can get it more information about the Spring course.


    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete