Upload
nikhilbhalwankar1883
View
226
Download
0
Embed Size (px)
Citation preview
8/17/2019 Data Retrieval using RESTFul Web Service (Hibernate + Spring + Maven)
1/14
Data Retrieval using
RESTFul Web Service
+Hibernate
(Maven project using Eclipse)
8/17/2019 Data Retrieval using RESTFul Web Service (Hibernate + Spring + Maven)
2/14
Introduction
This docuent e!plains ho" to "rite a RESTFul "eb service# The e!aple ta$en heresho"s ho" to select data %ro the database using Hibernate &ueries# The project is built
using eclipse with Spring framework and Maven.
Create maven project in Eclipse
'reate a ne" siple Maven roject in Eclipse# ou can search the docuentation %or thesae online#
Create pom.xml
4.0.0rest!ulrest!ul
0.$rest!ul
java.nethttp://do#nload.java.net/maven/&/
javax.servletservlet(api
&.*
8/17/2019 Data Retrieval using RESTFul Web Service (Hibernate + Spring + Maven)
3/14
org.spring!rame#or"spring('eans+,org.spring!rame#or".version-
org.spring!rame#or"spring(jd'c
+,org.spring!rame#or".version-
org.spring!rame#or"spring(#e'+,org.spring!rame#or".version-
org.spring!rame#or"spring(#e'mvc+,org.spring!rame#or".version-
org.spring!rame#or"spring(orm+,org.spring!rame#or".version-
org.spring!rame#or"spring(aspects+,org.spring!rame#or".version-
org.hi'ernatehi'ernate(entit%manager
..&.
tagli'sstandard$.$.&
javax.servletjstl$.$.&
m%s1lm%s1l(connector(java*.$.$0
commons(d'cpcommons(d'cp&0002&*.$244&2
commons(poolcommons(pool
&0002&*.$2343
javax.transaction
8/17/2019 Data Retrieval using RESTFul Web Service (Hibernate + Spring + Maven)
4/14
jta$.$
This should autoaticall* start do"nloading aven jar %iles related to the
po#!l entries# % not, then
-) right clic$ on project %ro project e!plorer.) select Run /s 01 Maven nstall
web.xml
rest!ul
index.html spring
8/17/2019 Data Retrieval using RESTFul Web Service (Hibernate + Spring + Maven)
5/14
org.spring!rame#or".#e'.servlet.@ispatcher6ervlet $ spring
/
spring-servlet.xml
This %ile contains in%oration about the dispatcher servlet "hich "ill run# t also has
re%erences to
•
Database connection properties• Hibernate con%iguration
• Message bundle (optional)
2elo" is ho" the %ile loo$s li$e#
8/17/2019 Data Retrieval using RESTFul Web Service (Hibernate + Spring + Maven)
6/14
class="org.#prngramewor.conte%t.#(pport.e)oa&a*)ee#o(rce(n&)eMe##age!o(rce">
/BC(8/hi'ernate(c!g.xml
org.hi'ernate.c!g.nnotationAon!iguration
true
8/17/2019 Data Retrieval using RESTFul Web Service (Hibernate + Spring + Maven)
7/14
8/17/2019 Data Retrieval using RESTFul Web Service (Hibernate + Spring + Maven)
8/14
import com.rest!ul.6ervice.ser@ata6erviceJ
KAontrollerpublic class ;estulAontroller ,
Kuto#iredser@ata6ervice user@ata6erviceJ
K;e1uest9appingvalue = Hdata;etrievalHM method = ;e1uest9ethod.@5T Npublic K;esponseCod%List get@atarom@ata'aseK;e1uestGaram 9ap
re1uestGaramsM Ittp6ervlet;esponse responseN ,
response.setAontent%peHapplication/jsonHNJresponse.set6tatus&00NJreturn [email protected]
--
2elo" is ho" the Rest%ul "eb service "ill be called,
http://server-ip!:"#"#/restful/dataRetrieval
$nce this url is hit with dataRetrieval as re%uest mapping& the associated methodget'ata(rom'atabase is called. )he method in turn invo*es a service which capturesdata from database and sends the data in +,$ format and sends to the place fromwhere the Restful web service is called.
serEntit.java 0Entit class mapped to database table1
3serEntit*#java is the class "hich is apped "ith the database table 3SER#
package com.rest!ul.ntit%J
import javax.persistence.AolumnJimport javax.persistence.ntit%Jimport javax.persistence.dJimport javax.persistence.a'leJ
Kntit%Ka'lename=H6;HNpublic class serntit% ,
KdKAolumnname=H@HM nulla'le=falseM length=*Nprivate int @J
KAolumnname=H89HM nulla'le=falseM length=*0Nprivate 6tring !irst8ameJ
KAolumnname=HL89HM nulla'le=falseM length=*0N
private 6tring last8ameJ
KAolumnname=H9LHM nulla'le=falseM length=*0Nprivate 6tring emailddressJ
8/17/2019 Data Retrieval using RESTFul Web Service (Hibernate + Spring + Maven)
9/14
/OO O @return the i@ O/public int get@N ,
return @J
-
/OO O @param i@ the i@ to set O/public void set@int i@N ,
@ = i@J-
/OO O @return the !irst8ame O/public 6tring getirst8ameN ,
return !irst8ameJ-
/OO O @param !irst8ame the !irst8ame to set O/public void setirst8ame6tring !irst8ameN ,
this.!irst8ame = !irst8ameJ-
/OO O @return the last8ame O/
public 6tring getLast8ameN ,return last8ameJ
-
/OO O @param last8ame the last8ame to set O/public void setLast8ame6tring last8ameN ,
this.last8ame = last8ameJ-
/OO O @return the emailddress O/public 6tring getmailddressN ,
return emailddressJ-
/OO O @param emailddress the emailddress to set O/public void setmailddress6tring emailddressN ,
this.emailddress = emailddressJ-
-
8/17/2019 Data Retrieval using RESTFul Web Service (Hibernate + Spring + Maven)
10/14
ser'ata,ervice.java and ser'ata,erviceImpl.java
These %iles a$e service la*er# Data access objects "hich interact "ith the database are
called %ro these services "hich in turn provide the data to the controller class#
UserDataService.java
package com.rest!ul.6erviceJ
import java.util.ListJimport com.rest!ul.ntit%.serntit%J
public interface ser@ata6ervice ,
public List getllsersNJ-
UserDataServiceImpl.java
package com.restful.Service;
import java.util.ListJ
import org.spring!rame#or".'eans.!actor%.annotation.uto#iredJimport org.spring!rame#or".stereot%pe.6erviceJimport org.spring!rame#or".transaction.annotation.ransactionalJ
import [email protected]@7J
import com.rest!ul.ntit%.serntit%J
K6ervicepublic class ser@ata6ervicempl implements ser@ata6ervice ,
Kuto#iredser@7 user@7J
KransactionalK7verridepublic List getllsersN ,
return [email protected]
}
8/17/2019 Data Retrieval using RESTFul Web Service (Hibernate + Spring + Maven)
11/14
ser'2$.java and ser'2$Impl.java
These data access objects counicate "ith database in order to retrieve the data using
hibernate %rae"or$#
UserDA.java
package comrestful!"#$
import java.util.ListJimport com.rest!ul.ntit%.serntit%J
public interface ser@7 ,
public List getllsersNJ-
UserDAImpl.java
package com.rest!ul.@7J
import java.util.ListJ
import org.hi'ernate.6essionactor%Jimport org.spring!rame#or".'eans.!actor%.annotation.uto#iredJimport org.spring!rame#or".stereot%pe.AomponentJimport org.hi'ernate.Duer%J
import com.rest!ul.ntit%.serntit%J
KAomponentHser@7HNpublic class ser@7mpl implements ser@7 ,
Kuto#iredprivate 6essionactor% sessionactor%J
K6uppressBarningsHunchec"edHNK7verridepublic List getllsersN ,
Duer% 1uer% = this.sessionactor%.getAurrent6essionN.createDuer%Hselect user@etails !rom serntit% user@etailsHNJ
List userList = nullJuserList = 1uer%.listNJ
return userListJ-
-
8/17/2019 Data Retrieval using RESTFul Web Service (Hibernate + Spring + Maven)
12/14
3aven pdate& Clean and Install
• Right clic$ on roject %ro project e!plorer
• Select Maven01 3pdate roject
• Right clic$ on project %ro project e!plorer
• Select Run /s 01 Maven 'lean
• Right clic$ on project %ro project e!plorer
• Select Run /s 01 Maven nstall
'ispatcher ,ervlet Error
There are chances that *ou "ill get belo" error#
6V;: 6ervlet /rest!ul thre# loadN exceptionjava.lang.Alass8otoundxception:org.spring!rame#or".#e'.servlet.@ispatcher6ervlet
2elo" is ho" to resolve it#
-) Right clic$ on roject.) Select Deplo*ent /ssebl*
4) /dd Maven Dependiencies directive
04o to next 5age1
8/17/2019 Data Retrieval using RESTFul Web Service (Hibernate + Spring + Maven)
13/14
5roject ,tructure
8/17/2019 Data Retrieval using RESTFul Web Service (Hibernate + Spring + Maven)
14/14
Chec* how Rest(ul 6eb ,ervice wor*s
nstall Mo5illa /ddon RE,)Client. 3sing this addon "e can siulate 6ET, 3T,DE7ETE, 8ST, 8T89S, HE/D etc#