Sunday, October 12, 2014

Creating a Maven Spring/Hibernate MVC Restful Web Service using Oracle database in Eclipse (Part 2)

The part 2 will show you how to setup spring oracle database connection by using Maven pom.xml

First create table in Oracle database, and insert 2 records. 

You need to manually install the Oracle jdbc jar in Maven local repository first (Maven central does not have this.)

For Oracle 11g, download ojdbc14.jar, and for Oracle 10g, download classes12.jar.
Install jar in Maven local repository:

$ mvn install:install-file -Dpackaging=jar -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dfile="c:\download\ojdbc14.jar"

Or manually put the downloaded file into the repository: .m2\repository\com\oracle\ojdbc14\10.2.0.4.0\, and rename it to ojdbc14-10.2.0.4.0.jar

For Oracle 10g
$ mvn install:install-file -Dpackaging=jar -DgroupId=com.oracle -DartifactId=classes12 -Dversion=10.2.0.2.0 -Dfile="c:\download\classes12.jar"

Or manually put the downloaded file into the repository: .m2\repository\com\oracle\class12\10.2.0.2.0\ and rename it to classes12-10.2.0.2.0.jar



Add dependency in pom.xml

<dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-jdbc</artifactId>
                  <version>4.0.5.RELEASE</version>
              </dependency>
For Oracle 11g

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc14</artifactId>
    <version>10.2.0.4.0</version>
</dependency>
For Oracle 10g
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>classes12</artifactId>
    <version>10.2.0.2.0</version>
</dependency>

Next, add dependency for JSON and Hibernate 

<dependency>
                     <groupId>org.codehaus.jackson</groupId>
                     <artifactId>jackson-mapper-asl</artifactId>
                     <version>1.9.10</version>
              </dependency>
             
              <dependency>
                     <groupId>org.hibernate</groupId>
                     <artifactId>hibernate-entitymanager</artifactId>
                     <version>4.3.5.Final</version>
              </dependency>

Update Maven dependencies, right click MyMavenWS project à Maven à Update Project



Next, add spring-servlet.xml under WEB-INF folder

spring-servlet.xml


Next,modify web.xml

web.xml


Next, create folder model under src\main\java\spring\code\com , and add Student.java

Student.java

Next, create dao folder , and add DataDao.java, DataDaoImpl.java

DataDao.java

DataDaoImpl.java


Next , add  RestController.java under controller folder

RestController.java

At final, the project structure is looks like this:





Run it on tomcat, in case port 8080 is used by Oracle XE, run Tomcat on 8081 port. Enter "tomcat:run -Dmaven.tomcat.port=8081" in goals at Run Configuration window.

Enter http://localhost:8081/MyMavenWS/student/list on browser. You can see the JSON data type of the student list. 






Below is for deleting.  






Next, create a client file to call web service,

Add test folder under src/main/jave/spring/code/com, and add TestClient.java

TestClient.java

Source Code is here: 



No comments:

Post a Comment