Simple Spring Data Rest Example

Spring Data Rest is a project is to provide a solid foundation on which to expose CRUD operations to your JPA Repository-managed entities using plain HTTP REST semantics. The first implementation of this exporter uses Spring MVC and the standard Servlet architecture to make it easy to deploy a WAR file that becomes a full-blown CRUD application simply by including your domain classes and their corresponding Repository definitions. It is very strong and useful because it can provide you RESTful webservices in a matter of minutes. In the example below I will show you a short overview of the capabilities of the framework.


Maven Dependencies

I will use maven for handling dependencies and building this example. Below I demonstrate the pom.xml file.


As Spring provides several ways to provide configuration, I will use the java config way.

I will use Servlet 3 Java Web Configuration instead of web.xml file. 

Let's configure the hibernate which wiil be ny database abstraction layer. I will do it in an other java file the AppConfiguration. I use for simplicity reasons and portability an embedded database the hsql.


A simple model from singer with their albums. Each singer has a list of albums.



Finally, the model I created above needs to be persitsted and exposed as a RESTful service. In order to succeed that, I need to use spring-data repositiories. A repository is actually a DAO implementation. It offers all the CRUD operations and pagination operations without any programming effort. I just define an empty interface and spring data does all the work. But still it is configurable and customizable. You can provide your queries and stuff like that.

The @RestResource annotation could be left out. But if i did, the path and rel would be named after the entity, which i dont want. A REST resource that contains multiple children should be named plural though.


Build and Run

The project now is ready. Firstly, we need to compile and package our code.

Let's run our RESTful service. As you see I have in pom.xml the jetty plugin. We will use jetty to deploy and test our service.


Testing the RESTful service


Let's test our service with a web client. Personally I use chrome's extension advanced rest client, but you can use any client you want.

GET http://localhost:8080/

Let's create a Singer!

POST http://localhost:8080/singers

At response headers we get Location: http://localhost:8080/singers/10 which is the path of our singer resource

Lets Create an Album. An album belongs to a singer.

POST http://localhost:8080/albums

At response headers we get Location: http://localhost:8080/albums/20 which is the path of our album resource.

Lets get all singers

GET http://localhost:8080/singers

Let's get all the albums

GET http://localhost:8080/albums

Moreover you can use all the RESTful operations as PUT DELETE and of course to get specific albums or singers by id.

You can find the source code available on


Vertical Tabs