Wednesday, October 12, 2011

Develop JAX-WS using eclipse, test using SOAPUI and deploy to Tomcat


Today I am going to create a jax-ws web service and deploy to tomcat. So lets start it …

Steps : -
1 ) First we need to download jax-ws jar that we can download it from here .

2) Copy that downloaded jar file to C:/ and then  run java -jar JAXWS2.1.1_20070501.jar

3) Now Open Eclipse and Create a dynamic web project. Name it as JAXWSCalculator

4) Create a class under Src and Name it as Calculator.java under package de.swapgmbh.service.calculate

   Here is the sample Calculator.java class

   package de.swapgmbh.service.calculate;
     
   import javax.jws.WebMethod;
   import javax.jws.WebService;
   @WebService
   public class Calculator {
   @WebMethod
   public int add(int expr1, int expr2) {
   System.out.println("Now adding " + expr1 + " and "+expr2);
   return expr1+expr2;
   }
   }

6) Now Create a folder named wsdl under WebContent/WEB-INF

7) Create a folder under your root project directory structure named generate.





8) Now Click on Run -> External Tools -> Configure External Tools


   Now create a New Configuration.

   Give Name as wsgen,

   Location: C:\jaxws-ri\bin\wsgen.bat (This you have downloaded 

   and copied into c:\)

   For Arguments:
  -verbose -wsdl -keep -r "D:\New      Worksapce1\JAXWSCalculator\WebContent\WEB-INF\wsdl" -d       "D:\New Worksapce1\JAXWSCalculator\generated" -cp D:\New Worksapce1\JAXWSCalculator\build\classes  ${java_type_name}


Here I have given the location of my workspace ex (D:\New 

Worksapce1\JAXWSCalculator\WebContent\WEB-INF\wsdl). You need to 

give the location of your workspace.


9)In environment tab create this two variable,
JAVA_HOME-C:\jdk1.5.04 (Path of you JDK)
JAXWS_HOME - C:\jaxws-ri (This is you download and kept in C:\)





10) Now click apply


11) Modify the web.xml File

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 <display-name>JAXWSCalculator</display-name>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<description>JAX-WS endpoint - calculator</description>
<display-name>JAXWSCalculator</display-name>
<servlet-name>calculator</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>calculator</servlet-name>
<url-pattern>/addnumbers</url-pattern>
</servlet-mapping>
</web-app>

12) Create a new file sun-jaxws.xml under WEB-INF
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
<endpoint name="calculator" implementation="de.swapgmbh.service.calculate.Calculator" url-pattern="/addnumbers"/>
</endpoints>



13)  Now build the project using Project -> Build All

14) Now run wsgen that we have recently created.

15) Check wsdl that is generated and change the location attribute of <soap location: “”> as http://localhost:8080/JAXWSCalculator/

Now copy all the jar files from C:\jaxws-ri\lib to lib directory of your project.

Now  Export the project as war and copy to webapp directory of the tomcat.

Start tomcat and type http://localhost:8080/JAXWSCalculator/addnumbers?wsdl