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:
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.
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:
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”.
For more Info on Spring Rest Shell: https://github.com/spring-projects/rest-shell