Exporting Spring Data JPA Repositories as REST Services using Spring Data REST

Spring Data modules provides various modules to work with various types of datasources like RDBMS, NOSQL stores etc in unified way. In my previous article  SpringMVC4 + Spring Data JPA + SpringSecurity configuration using JavaConfig I have explained how to configure Spring Data JPA using JavaConfig.

Now in this post let us see how we can use Spring Data JPA repositories and export JPA entities as REST endpoints using Spring Data REST.

First let us configure spring-data-jpa and spring-data-rest-webmvc dependencies in our pom.xml.

Make sure you have latest released versions configured correctly, otherwise you will encounter the following error:
java.lang.ClassNotFoundException: org.springframework.data.mapping.SimplePropertyHandler

Create JPA entities.

Configure DispatcherServlet using AbstractAnnotationConfigDispatcherServletInitializer.

Observe that we have added RepositoryRestMvcConfiguration.class to getServletConfigClasses() method.
RepositoryRestMvcConfiguration is the one which does the heavy lifting of looking for Spring Data Repositories and exporting them as REST endpoints.

https://gist.github.com/sivaprasadreddy/9303094 /]

Create Spring Data JPA repositories for JPA entities.

That’s it. Spring Data REST will take care of rest of the things.

You can use spring Rest Shell https://github.com/spring-projects/rest-shell or Chrome’s Postman Addon to test the exported REST services.


Now we can change the baseUri using baseUri command as follows:
http://localhost:8080:>baseUri http://localhost:8080/spring-data-rest-demo/rest/

rel         href
users       http://localhost:8080/spring-data-rest-demo/rest/users{?page,size,sort}
roles       http://localhost:8080/spring-data-rest-demo/rest/roles{?page,size,sort}
contacts    http://localhost:8080/spring-data-rest-demo/rest/contacts{?page,size,sort}

Note: It seems there is an issue with rest-shell when the DispatcherServlet url mapped to “/” and issue list command it responds with “No resources found”.

http://localhost:8080/spring-data-rest-demo/rest/>get users/

You can find the source code at https://github.com/sivaprasadreddy/sivalabs-blog-samples-code/tree/master/spring-data-rest-demo
For more Info on Spring Rest Shell: https://github.com/spring-projects/rest-shell


  1. Jarvis
      April 15, 2014

    Hello Nice Tutorial,

    However could you show us how do you manage date format and supporting i18n?

    Thank you

  2. Siva Prasad Reddy
      April 15, 2014

    Here I used Jackson json library to marshalling/unmarshalling objects to/from json. So to customize Date formats we need to register Custom DateSerializer, Desirializer. For i18n we are just getting data from DB and converting it into JSON, no additional config is required if your data in DB is in desired language.

  3. Jarvis
      April 16, 2014

    Thank you Siva,
    I found an example solution on http://loianegroner.com/2010/09/how-to-serialize-java-util-date-with-jackson-json-processor-spring-3-0/ however this should be improved. It's not the best and I believe it should be improved by setting the formatter based on the Locale of the client.
    Now I am trying to solve another problem related to the uniqueness of an entity on a field. http://stackoverflow.com/questions/3495368/unique-constraint-with-jpa-and-bean-validation/3499111#3499111 I found how to solve it with jboss http://lucasterdev.altervista.org/wordpress/2012/07/28/unique-constraint-validation-in-jpa-part-2/ but I would like to write less code.

  4. deepthi
      July 29, 2014

    Hi Siva

    Above json output how to convert it to user object and above json data dosent contain the roles and contact information. can you pls explain

  5. Chandrasekhar Timmapuram
      November 19, 2015

    Hi Siva

    please resolve this
    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException

Leave a Reply

Your email address will not be published. Required fields are marked *