JAX WS Hello World

In this article, we will create a JAX WS Hello World program.

 

Tools/Technologies Used :

  • Eclipse IDE
  • Tomcat Server
  • Metro jars

 

Here is an overview of the steps involved :

  1. Create a Dynamic web project
  2. Add Web service class
  3. Define WSServlet and url-mapping in web.xml
  4. Add sun-jaxws.xml and provide reference to the webservice class
  5. Add Metro jars
  6. Deploy and Test

 

Step 1: (Create Dynamic Web project)

In Eclipse, create a dynamic web project using :

File- > New -> Dynamic Web Project

 

Name the project as JAXWSHelloWorld.

 

Step 2: (WebService class)

Create a package com.topjavatutorial.jaxws in the src folder.

Add the below class HelloWorld in it.

 

package com.topjavatutorial.jaxws;

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

@WebService
public class HelloWorld {
  @WebMethod
  public String hello(String name){
    return "Hello " + name;
  }
}

Step 3: (web.xml changes)

 

Since we created a dynamic web project, web.xml file should be available under WEB-INF folder.

 

If you don’t see, you can create it with below steps:

  • Right click on the project.
  • Select JAVA EE Tools -> Generate Deployment Descriptor Stub

 

Configure the class WSServletContextListener as listener and WSServlet as the servet class. Also, add url-mapping in web.xml as follows:

 

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>JAXWSHelloWorld</display-name>
   <listener>  
     <listener-class>  
        com.sun.xml.ws.transport.http.servlet.WSServletContextListener  
     </listener-class>  
   </listener>  
   <servlet>  
      <servlet-name>jaxws</servlet-name>  
      <servlet-class>  
        com.sun.xml.ws.transport.http.servlet.WSServlet  
      </servlet-class>  
   </servlet>  
   <servlet-mapping>  
     <servlet-name>jaxws</servlet-name>  
     <url-pattern>/*</url-pattern>  
   </servlet-mapping>  

</web-app>

 

Step 4 : (sun-jaxws.xml)

Add following sun-jaxws.xml in WEB-INF folder.
 

<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">  
  <endpoint  
     name="Hello"  
     implementation="com.topjavatutorial.jaxws.HelloWorld"  
     url-pattern="/*"/>  
</endpoints>

 

Step 5 : (Metro jars)

 

Download Metro jars from the Metro project website metro.java.net.

 

The classes WSServletContextListener and WSServlet referred in web.xml are present in Metro jar file webservices-rt.jar

 

Copy the jar files webservices-rt.jar and webservices-api.jar into your project lib folder.

 

Here is how the project structure looks like at this point :
 
jax ws project structure
 

Step 6 : (Deploy and Test)

 

Deploy the project on Tomcat server.

 

Once the webservice is deployed,  you can verify the service status using following url in browser:

http://localhost:8080/JAXWSHelloWorld/jaxws

 

jax ws service

The WSDL can be accessed using :

http://localhost:8080/JAXWSHelloWorld?wsdl

 

The xml schema associated with this service can be accessed using :

http://localhost:8080/JAXWSHelloWorld?xsd=1

 

JAX WS xml schema

 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags