Article describes setting up JAX-WS using Maven and various ways to deploy the webservice . Creating WS using JAX-WS is super simple , simply annotate the class  with @WebService and its basically done. Simpicity is good not only for development but also creating mock services and testing error cases.
Maven makes it   more reusable be able to  distibute easily to developers. Also maven takes care of wsgen and  dependent libraries.
I use Tomcat 7 and running Java 6 . If you are using Java7  , wsgen fails ( yeah it has to be fixed)  , so till then configure the IDE to use Java6 for the project
  
Java Code - Your WebService ( Simple .. basically you can return POJO or collection of objects for practical use case)
folder - src/main/java
folder - project folder
Running it using EndPoint
Using Endpoint just use the publisher and WebService is running .
test service : http://localhost:888/TestWS/MyService?wsdl
Running it in Tomcat.
My preferred method is to run it in tomcat . You will need couple of more files
web.xml
folder :src/webapp/WEB-INF
sun-jaxws.xml
folder :src/webapp/WEB-INF
Deploy the war file to tomcat
test deployment : http://localhost:8080/TestWS/myservice?wsdl
Java Code - Your WebService ( Simple .. basically you can return POJO or collection of objects for practical use case)
folder - src/main/java
package com.services;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public class MyService {
 @WebMethod
 public String sayHello()
 {
  return "Hello World";
 }
}
Pom.xmlfolder - project folder
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.myws.testws</groupId>
 <artifactId>TestWS</artifactId>
 <packaging>war</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>Test Webservice</name>
 <url>http://maven.apache.org</url>
 
 <dependencies>
    <dependency>
  <groupId>com.sun.xml.ws</groupId>
  <artifactId>jaxws-rt</artifactId>
  <version>2.1.3</version>
  </dependency>
 </dependencies>
 <build>
  
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.2</version>
     <configuration>
     <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
   </plugin>
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxws-maven-plugin</artifactId>
    <version>1.7</version>
    <executions>
     <execution>
      <id>generate-wsdl</id>
      <phase>process-classes</phase>
      <goals>
       <goal>wsgen</goal>
      </goals>
      <configuration>
       <sei>com.services.MyService</sei>
       <genWsdl>true</genWsdl>
      </configuration>
     </execution>
    
    </executions>
   </plugin>
   
  </plugins>
 </build>
</project>
Running it using EndPoint
Using Endpoint just use the publisher and WebService is running .
package com.services;
import javax.xml.ws.Endpoint;
 
public class MyServicePublisher{
 
 public static void main(String[] args) {
    Endpoint.publish("http://localhost:8888/TestWS/MyService", new MyService());
    }
 
}
test service : http://localhost:888/TestWS/MyService?wsdl
Running it in Tomcat.
My preferred method is to run it in tomcat . You will need couple of more files
web.xml
folder :src/webapp/WEB-INF
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd"> <web-app> <listener> <listener-class> com.sun.xml.ws.transport.http.servlet.WSServletContextListener </listener-class> </listener> <servlet> <servlet-name>myservice</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>myservice</servlet-name> <url-pattern>/myservice</url-pattern> </servlet-mapping> <session-config> <session-timeout>120</session-timeout> </session-config> </web-app>
sun-jaxws.xml
folder :src/webapp/WEB-INF
<?xml version="1.0" encoding="UTF-8"?> <endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0"> <endpoint name="MyService" implementation="com.services.MyService" url-pattern="/myservice"/> </endpoints>
Deploy the war file to tomcat
test deployment : http://localhost:8080/TestWS/myservice?wsdl
 
 
 
No comments:
Post a Comment