Friday, September 30, 2011

Deploying JAX-WS WebService on Tomcat-6

Now we are going to see how to deploy JAX-WS WebService on Tomcat Server.
We are going to deploy The AuthenticationService developed in http://sivalabs.blogspot.com/2011/09/developing-webservices-using-jax-ws.html on apache-tomcat-6.0.32.

To deploy our AuthenticationService we need to add the following configuration.

1.web.xml

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

2. Create a new file WEB-INF/sun-jax-ws.xml

<?xml version="1.0" encoding="UTF-8"?>
<endpoints
  xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
  version="2.0">
  
  <endpoint
      name="AuthenticationService"
      implementation="com.sivalabs.caas.services.AuthenticationServiceImpl"
      url-pattern="/services/AuthenticationService"/>
      
</endpoints>

3. Download the JAX-WS Reference Implementation from http://jax-ws.java.net/
Copy all the jar files from jaxws-ri/lib folder to WEB-INF/lib.


Now deploy the application on Tomcat server.
You don't need to publish the Service by our-self as we did using EndpointPublisher.
Once the tomcat is up and running see the generated wsdl at http://localhost:8080/CAAS/services/AuthenticationService?wsdl.

Now if you test the AuthenticationService using standalone client it will work fine.
public static void testAuthenticationService()throws Exception
{
  URL wsdlUrl = new URL("http://localhost:8080/CAAS/services/AuthenticationService?wsdl");
  QName qName = new QName("http://sivalabs.blogspot.com/services/AuthenticationService", "AuthenticationService");
  Service service = Service.create(wsdlUrl,qName);
  AuthenticationService port = service.getPort(AuthenticationService.class);
  Credentials credentials=new Credentials();
  credentials.setUserName("admin");
  credentials.setPassword("admin");
  AuthenticationStatus authenticationStatus = port.authenticate(credentials);
  System.out.println(authenticationStatus.getStatusMessage());
}

But if you try to test with the wsimport tool generated client code make sure that you dont have jax-ws-ri jars in Client classpath.
Otherwise you will get the below error:

Exception in thread "main" java.lang.NoSuchMethodError: javax.xml.ws.WebFault.messageName()Ljava/lang/String;
 at com.sun.xml.ws.model.RuntimeModeler.processExceptions(RuntimeModeler.java:1162)
 at com.sun.xml.ws.model.RuntimeModeler.processDocWrappedMethod(RuntimeModeler.java:898)

1 comment:

  1. Two Things 1) I see the CAAS in your publish class, but when you move away from that to the Web App/Tomcat, I don't see where it's coming into play, but it still seems to be part of the URL you're using to import your client. 2) Shouldn't the name of the additional .xml file be sun-jaxws.xml rather than sun-jax-ws.xml?

    I seem to be getting web.xml and all the rest linked up, but can't find the WSDL when I enter the URL and it's not popping up in Web Services Explorer.

    ReplyDelete