Sunday 3 May 2015

Create JAX-WS Web Service with Top down approach from scratch using maven (Java to WSDL)

Create Sample Web Project using maven eg.

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=com.yogesh.patil -DartifactId=ConcatService -DinteractiveMode=false

Project structure must be created in current directory as follow.


Now start creating service implementation. For ease import this project in eclipse as follow.
Select Maven project to import in eclipse as follow ( If maven in not installed in your eclipse here are Steps to configure Maven in eclipse)


Now Select "Existing Maven Projects" option to import project.


Browse and select "ConcatService" project generated using maven. Now click on "Finish" to import project.
Now you can see imported "ConcatService" project in maven.


Create new Interface and implementation class with annotations which is to be exposed as web service as follow.

Interface.

package com.yogesh.patil;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface ConcatService {

    @WebMethod
    public String concatString (String str1, String str2);
}

Implementation class.

package com.yogesh.patil;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.ParameterStyle;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;

@WebService(name="ConcatServiceImpService")
// Default biding Document/literal/wrapped
@SOAPBinding(style = Style.DOCUMENT, use=Use.LITERAL,parameterStyle=ParameterStyle.WRAPPED)
public class ConcatServiceImpl implements ConcatService {

    public String concatString (String str1, String str2) {
        return str1 + str2;
    }
}


From @SOAPBinding annotation on Web service class we can say this is Document/literal type web service with parameter types wrapped.

Now configure Apache CXF Plugin to generate WSDL from Java class using java2wsdl goal provided by this plugin.


<plugin>
 <groupId>org.apache.cxf</groupId>
 <artifactId>cxf-codegen-plugin</artifactId>
 <version>2.0.9</version>
  <dependencies>
   <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>2.0.9</version>
   </dependency>
  </dependencies>
  <executions>
   <execution>
    <id>generate-wsdl</id>
    <phase>process-classes</phase>
    <configuration>
     <outputFile>src/main/resources/WSDL/ConcatServiceImp.wsdl</outputFile>
     <className>com.yogesh.patil.ConcatServiceImp</className>
    </configuration>
    <goals>
     <goal>java2wsdl</goal>
    </goals>
   </execution>
  </executions>
</plugin>

Note- update highlighted part.
Download complete POM.

Create and configure JAX-WS deployment descriptor sun-jaxws.xml  in {PROJECT_HOME}\src\main\webapp\WEB-INF directory as follow.

NOTE - {PROJECT_HOME} indicates the location of project directory. This directory must contain pom.xml in our case.

<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
 version="2.0">
 <endpoint name="ConcatServiceImpl"
  implementation="com.yogesh.patil.ConcatServiceImpl"
  url-pattern="/ConcatService" />
</endpoints>

Update web application configuration {PROJECT_HOME}\src\main\webapp\WEB-INF\web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <listener>
        <listener-class>
                com.sun.xml.ws.transport.http.servlet.WSServletContextListener
        </listener-class>
    </listener>
    <servlet>
        <servlet-name>ConcatService</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>ConcatService</servlet-name>
        <url-pattern>/ConcatService</url-pattern>
    </servlet-mapping>
</web-app>

Add JAX-RT (Runt time) dependency to pom.xml

<!-- JAX-WS RT Dependency -->
<dependency>
 <groupId>com.sun.xml.ws</groupId>
 <artifactId>jaxws-rt</artifactId>
 <version>2.2.8</version>
</dependency>

Now execute $mvn clean install command. This command create .war file and also generate the WSDL file at mentioned location in plugin "<outputFile>src/main/resources/WSDL/ConcatServiceImp.wsdl</outputFile>"

Deployment file must be created in "{PROJECT_HOME}\target\ConcatService.war"


ConcatService\WEB-INF\classes\com\yogesh\patil\jaxws\ConcatString.class (JAWS generated request class)
ConcatService\WEB-INF\classes\com\yogesh\patil\jaxws\ConcatStringResponse.class (JAWS generated response class)
ConcatService\WEB-INF\classes\com\yogesh\patil\ConcatService.class
ConcatService\WEB-INF\classes\com\yogesh\patil\ConcatServiceImpl.class
ConcatService\WEB-INF\classes\WSDL\ConcatServiceImp.wsdl
ConcatService\WEB-INF\web.xml
ConcatService\WEB-INF\sun-jaxws.xml
ConcatService\index.jsp

Following are list of jar present in ConcatService\WEB-INF\lib, there are some jar which are not being used in our current code but these default jar's coming with JAXWS-RT dependency.



Copy ConcatService.war file to {TOMCAT_HOME}/webapps directory. Now start your tomcat server and access the deployed web service at following URL (assuming tomcat is configured to run on 8080 port)

"http://localhost:8080/ConcatService/ConcatService"

You can see deploymet as follow.



Now your web service is ready for testing. You test it by creating web service client or simply by Using tools like SOAP UI. Create new SOaP UI project.


Create Web service client using deployed Web service URL.


Click Ok. Now you are ready to invoke and test your web service.


now submit this request. You will get the response as follow.


You can download:

Some Useful links :

Software's used for this tutorial:
  • Windows 7 (64 bit)
  • JDK 1.6
  • Apache Tomcat 6.0.35
  • Apache CXF maven plugin v2.0.9
  • JAX-WS RT (Run time) v2.2.8
  • Development tool - Eclipse Kepler
Suggestions and recommendations are welcome. :)

4 comments: