Wednesday, March 16, 2011

JAX-WS Hello World Example

    In this article I provide a step-by-step illustrated example on creating a simple Hell World Service example by using JAX-WS.

Service – Hello World service exposing one method called greet. The method implementation accepts a first name and a last name and returns a greeting string.

Stack – JAX-WS with Glassfish Server, Eclipse for development.


Step 1: Create a Dynamic Web Project in eclipse, with the name "JAXWSHelloWorld".

Step 2: Create a simple implementation class called com.jaxws.example.HelloWorldEndpoint. It will contain a single public method called greet() which accepts 2 strings (firstname and lastName) and returns a greeting message. Then annotate the class with the @WebService annotation and the greet method with @WebMethod annotation.

The @WebService has the serviceName attribute. The value of this attribute is the name of the service in the WSDL and also the URL at which the service is available.

The @WebMethod has the attribute operationName. The value of this attribute is the name of the operation defined in the WSDL.

Please note these are not mandatory and are used only for reference/ease of debugging.

The sample source is given below.

Note: The values attributes of annotation are optional. More details on what they mean will be explained later.

Step 3: Create the related resources (JAXB binding files, WSDL and schema files and corresponding classes). For the run the wsgen bat available with JDK 1.6 or Glassfish with the following parameters:

wsgen –verbose –cp build/classes –s src –wsdl –r WebContent\WEB-INF\wsdl –d build\classes com.jaxws.example.HelloWorldEndpoint

-verbose : prints the operation being executed

- cp <folder> : the location of the base folder where the class file of the Endpoint (HelloWorldEndpoint) is located.

-s <folder> : the location where the generated source files (JAXB binding) are stored

-wsdl : specifies that the WSDL should be generated

-r <folder> : the location of the folder where the resources/artifacts will be generated

-d <folder> : the location of the folder where the generated class files (of JAXB Binding and JAXWS files) will be kept.

Step 4: Deploy the application on the server (Glassfish). To deploy the application, import the glassfish server into eclipse and then just simply right click the project choose Run As > Server. When the server starts monitor the logs. In the logs you will see a line about the WebService Endpoint successfully deployed and the location of the endpoint.

Note: The location is of the format -> http://url:portNumber/applicationName/serviceName[Of Endpoint]

Step 5: The web service is deployed. Hit the URL of the endpoint specified during deployment [http://locahost:8080/JAXWSHelloWorld/HelloWorld] in my case. You should be able to see a dynamically generated page detailing the service and operations exposed.

Step 6: The Web service is now deployed. To confirm check the WSDL deployed the link for which is available on the generated screen. [http://localhost:8080/JAXWSHelloWorld/HelloWorld?wsdl] in my case.

This is a generated WSDL by the JAXWS implementation (Glassfish in my case). The Service is of the type document/literal [wrapped]. The XSD is also available.

Step 7: To test the service, use Web Service Explorer available with eclipse or SOAPUI and set the endpoint to the location of the endpoint. The sample request response in my case are as given below.

 <soapenv:Envelope xmlns:soapenv="" xmlns:q0="" xmlns:xsd="" xmlns:xsi="">  

 <S:Envelope xmlns:S="">  
    <ns2:greetResponse xmlns:ns2="">  
      <return>Hello AdityaKeyal</return>  

The above example is available for download here. It is a complete eclipse project. Just download and import into eclipse and build then deploy on Glassfish for it to run.


  1. Kewl dude.. I went through all your articles. Very well articulated. It would be great if u can put up enterprise service bus here, where,how,why ESB fits in.

  2. thanks dude.. definitely will do that. Just give me some time. Want to cover some principles of JAX-WS first.

  3. Simple and informative, thanks.