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: 



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

This tutorial shows you how to create a Maven Spring/Hibernate MVC Restful Web Service using Oracle database. The project uses the Maven archetype called spring-mvc-archetype.
In Eclipse IDE (Luna SR1), click menu File > New > Maven Project (or File > New > Other > Maven Project). The New Maven Project window appears:


Click Next


Click Next. In the next screen, Select an Archetype, type spring-mvc into the Filter textbox, as shown below:



If you don’t see the spring-mvc-archetype, click the Add Archetype… button. In the Add Archetype dialog, type the following information:

·         Archetype Group Id: co.ntier
·         Archetype Artifact Id: spring-mvc-archetype
·         Archetype Version: 1.0.2
·         Repository URL: http://maven-repository.com/artifact/co.ntier/spring-mvc-archetype/1.0.2






Click OK and wait for Eclipse downloading the archetype. After you see the spring-mvc-archetype in the list. Select it and click Next. In the next window, Specify Archetype parameters, enter the following information:
·         Group Id: MyMavenWS (for example)
·         Artifact Id: MyMavenWS (for example)
·         Version: 0.0.1-SNAPSHOT (Default)
·         Package: spring.code.com (for example)



Click Finish and Eclipse will generate the project. Finally we have a project structure looks like below:



*Note: If you got below error at this point, change Repository URL to: Repository: http://repo.maven.apache.org/maven2/
Unable to create project from archetype [co.ntier:spring-mvc-archetype:1.0.2 -> http://maven-repository.com/artifact/co.ntier/spring-mvc-archetype/1.0.2]. The defined artifact is not an archetype

·         Archetype Group Id: co.ntier
·         Archetype Artifact Id: spring-mvc-archetype
·         Archetype Version: 1.0.2
·         Repository URL: http://repo.maven.apache.org/maven2/

Now, it is time to run the the project. Right click on the pom.xml, and go to Run As à Run Configurations.




In the Create, mange, and run configurations screen. Double click Maven Build,  and create a new configuration.



Give the configuration a Name MyMavenWS_configuration (for example)
Click Browse Workspace, select the Base Directory to be the current project (MyMavenWS).
In the Goals, enter tomcat:run



 Apply and Run the configuration, you should see below server information at Console.


Open a browser, and type http://localhost:8080/MyMavenWS/, which should bring up a  “Hello World!” page as seen below: