Dec 28, 2013

Spring Data REST - Sample application


In this post I will discuss how to create a sample MVC application with RESTful services and how to consume them. I am using Spriing 3.2.
Spring Data Rest : Export JPA enitites to web.
This application is not a complete application, but I will prepare an application, make it running, and we will get success response, persistence, and transnational.

I refrerred - http://www.petrikainulainen.net/spring-data-jpa-tutorial/
for guidance and created project using project shared on above site.

Major Steps include -
1. Install Maven, Eclipse, and tomcat
2. Create Maven project, and make it dynamic.
3. Complete code.
4. Deploy and run.
Step 1 is lengthy and Step 3 is tough.

Lets assume you have completed Step 1 and 2. And maven path is set in Eclipse.
Window->Preference->Maven->Installations
and also Window->Preference->Maven->User Settings
And move to Step 3. And create a maven project.
I named it SpringDataRest. Right Click -> Properties -> Project Facets -> Check "Dynamic Web Module".
And my package structure is like -

com.spring.datajpa.config
 - to hold configuration java files

com.spring.datajpa.controller
 - to hold controllers

com.spring.datajpa.dto
- to hold DTO, which is passed to UI

com.spring.datajpa.model
- to hold entities which are mapped to DB tables

com.spring.datajpa.repository
- to hold Spring Data repository. One repository for one entity

com.spring.datajpa.service
- service layer

Inside src/main/resources we have properties.





com.spring.datajpa.config
In Spring 3.2, we don't need web.xml. We do all configuration in java class.  WebApplicationInitializer - Interface to be implemented in Servlet 3.0+ environments in order to configure the ServletContext programmatically as opposed to traditional web.xml approach.
DataJPAInitializer - extends WebApplicationInitializer, DispatcherServlet is created and initialized. Also ApplicationContext.class is registered. ApplicationContext must have @Configuration
ApplicationContext - Configures Spring application context - DataSource, Transaction Manager, Entity Manager Factory, Message Source resource bundle, and ViewResolver.

I know it looks bit extensive when I have to create multiple classes for one entity - Person, but its like that in Spring Data -
Person - @Entity, @Table(name="persons")
PersonRepository - extends JPARepositiry; this provides CRUD behavior to entity.
PersonDTO - A DTO object which is used as a form object in create person and edit person forms.
PersonController - Dispatcher servlet will forward all requests to controller. Controller operations are mapped to HTTP actions.
PersonService  class exposes operation over Person.

I have updated the server.xml in tomcat: path="/" for SpringDataRest.

So, communication happens like -
JSP - Controller - Service - Repository - DB.
H2 is used as DB, and Hibernate as JPA provider.

Since, this is my first application using Spring data, I have just followed the conventions and suggestions.  But this application makes familiar withe whole Spring setup : Spring Data, Spring MVC, and REST service.

I have uploaded the Eclipse workspace. I believe just any one needs to download the zip file and unzip and make it the eclipse workspace. Check server settings, like tomcat installation path, port should be 8080.
Please download the workspace form below location -
https://www.dropbox.com/s/a8dpxqhwwebbzg1/SpringData.zip



Share:

No comments

Post a Comment

Comment

© Shift, ShEkUP, Shape, and Surprise | All rights reserved.
Blogger Template Crafted by pipdig