Using Spring Data REST To Easily Create RESTful Endpoints For Data Stored In A Database


Spring Data is an ambitious project designed to make data access even simpler.  Spring Data consists of several sub-projects such as Spring Data JPA, which makes it easier for coding an application that uses JPA.  I had previously written about Spring Data JPA and found it significantly reduced the amount of code I had to write.

A relatively new addition to the Spring Data family of projects is Spring Data REST, which simplifies exposing JPA-based repositories as RESTful endpoints.  After reading Vijay Rawat's excellent blog article on how to use Spring Data REST I decided to modify the Struts 2 – Spring Data JPA example application I used in my Spring Data JPA blog article to include Spring Data REST.

Example Application

You can download the example application here.  To understand the example you may want to read my previous Spring Data JPA blog article.  This example uses both Spring Data JPA to reduce the data access coding and Spring Data REST to provide RESTful endpoints that this application and others may use to get Person records or to add a Person record to the database.

After downloading the example application, unzip it.  It uses Maven, so you should be able to import it into any Maven aware Java IDE.

View the README.txt file that is in the project's root folder for how to build and run the application.

Enabling Spring Data REST

There were just a few places I had to add/change code to enable Spring Data REST.  In pom.xml I had to add the dependencies for Spring Data REST and for Jackson (a library for processing JSON data). 

In web.xml I had to add this servlet and servlet-mapping:



Note the value for the url-pattern: /rest/*.  That url-pattern will govern how to write the RESTful endpoint. 

In class I had to move the @Id and @GeneratedValue annotations from the setEmplid method to the emplid field.  Spring Data REST would not work correctly if I used those annotations on the set method.

Spring Data REST and the JPA Repository

You should consult the Spring Data REST page for the complete explanation of how it works.  As I understand it Spring Data REST takes any JPA repositories I have (in the example there is just one – PersonRepository) and exposes it as a RESTful endpoint.  The REST resource will be named person.

Since I defined the url-pattern in web.xml for the exporter servlet-mapping to be /rest/* the REST endpoint for getting a specific person is: 


where the 3 is the emplid of the person I want to find.

I can use curl to test the RESTful endpoints

I can add a new person by using this curl command:

curl -v -d '{"first_name" : "Steve", "last_name" : "Smith"}' -H "Content-Type: application/json" http://localhost:8080/strutsspringdataexample/rest/person

To get all the Person objects from the database I can use:

curl -v http://localhost:8080/strutsspringdataexample/rest/person


Spring Data REST is at the release candidate 3 stage as of September 14, 2012, so a GA release should be coming soon.  Check the Spring Data REST home page for the latest version.

Spring Data REST, like the other Spring Data projects, makes it much easier for developers to create applications that interact with data.  Using Spring Data REST you can easily add a set of RESTful APIs to your application with minimal additional code.

Related Blog Entries

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version Contact Blog Owner