Nikos Maravitsas

About Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

JAX-WS Hello World Example – RPC Style

In this example we are going to see how to create, deploy and consume Web Services using JAX-WS. JAX-WS is a fine tool for creating Web Services and it’s included in the JDK since JDK 1.6.

You are going to see how easy it is to create and deploy RPC Web Services. What you need to run this example is only JDK 1.6 or above and that’s that.

The basic architecture of a JAX-WS Web Service consists of two main parts:

  1. A Web Service Endpoint: It is a connection point where pages and Web Services are exposed to consumers and clients.
  2. A Web Service Client: The program that makes use of the published Web Service from the above Endpoint.

1. JAX-WS Web Service End Point

In order to create a Web Service Endpoint:

  • First you have to create a Web Service Endpoint Interface. This interface will contain the declerations of all the methods you want to include in the Web Service.
  • Then you have to create a class that actually implements the above interface, which will be your Endpoint implementation.
  • Finally you create your Endpoint publisher which actually deploys the web service and creates and publishes the endpoint for the specified implementor object at a given address. The necessary server infrastructure will be created and configured by the JAX-WS implementation. You have to run the publisher to make your Web Service available to clients.

Web Service Endpoint Interface

WebServiceInterface.java:

package com.javacodegeeks.enterprise.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style = Style.RPC)
public interface WebServiceInterface{

	@WebMethod String getHelloWorldAsString(String str);

}

Web Service Endpoint Implementation

WebServiceImpl.java:

package com.javacodegeeks.enterprise.ws;

import javax.jws.WebService;

@WebService(endpointInterface = "com.javacodegeeks.enterprise.ws.WebServiceInterface")
public class WebServiceImpl implements WebServiceInterface{

	@Override
	public String getHelloWorldAsString(String str) {
		return "Hello World of JAX-WS " + str;
	}

}

Web Service Endpoint Publisher

WebServicePublisher.java:

package com.javacodegeeks.enterprise.ws;

import javax.xml.ws.Endpoint;
import com.javacodegeeks.enterprise.ws.WebServiceImpl;

public class WebServicePublisher{

	public static void main(String[] args) {
	   Endpoint.publish("http://localhost:8888/webserive/helloworld", new WebServiceImpl());
    }

}

Now when you run the publisher the Web Service will be available to the clients, deployed in the URL:

http://localhost:8888/webserive/helloworld

If you put that address to your browser, you should get this :

webservice-browser

And this is the wsdl file that is automatically created (published in http://localhost:8888/webserive/helloworld?wsdl):

<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is 
	JAX-WS RI 2.2.4-b01. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is 
	JAX-WS RI 2.2.4-b01. -->
<definitions
	xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
	xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy"
	xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://ws.enterprise.javacodegeeks.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.enterprise.javacodegeeks.com/"
	name="WebServiceImplService">
	<types />
	<message name="getHelloWorldAsString">
		<part name="arg0" type="xsd:string" />
	</message>
	<message name="getHelloWorldAsStringResponse">
		<part name="return" type="xsd:string" />
	</message>
	<portType name="WebServiceInterface">
		<operation name="getHelloWorldAsString">
			<input
				wsam:Action="http://ws.enterprise.javacodegeeks.com/WebServiceInterface/getHelloWorldAsStringRequest"
				message="tns:getHelloWorldAsString" />
			<output
				wsam:Action="http://ws.enterprise.javacodegeeks.com/WebServiceInterface/getHelloWorldAsStringResponse"
				message="tns:getHelloWorldAsStringResponse" />
		</operation>
	</portType>
	<binding name="WebServiceImplPortBinding" type="tns:WebServiceInterface">
		<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
			style="rpc" />
		<operation name="getHelloWorldAsString">
			<soap:operation soapAction="" />
			<input>
				<soap:body use="literal" namespace="http://ws.enterprise.javacodegeeks.com/" />
			</input>
			<output>
				<soap:body use="literal" namespace="http://ws.enterprise.javacodegeeks.com/" />
			</output>
		</operation>
	</binding>
	<service name="WebServiceImplService">
		<port name="WebServiceImplPort" binding="tns:WebServiceImplPortBinding">
			<soap:address location="http://localhost:8888/webserive/helloworld" />
		</port>
	</service>
</definitions>

2. Java Web Service Client

This is client that consumes the above Web Service, written in Java:

WebServiceClient.java:

package com.javacodegeeks.enterprise.ws.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.javacodegeeks.enterprise.ws.WebServiceInterface;

public class WebServiceClient{

	public static void main(String[] args) throws Exception {

	    URL wsdlUrl = new URL("http://localhost:8888/webserive/helloworld?wsdl");

	    //qualifier name ...
        QName qname = new QName("http://ws.enterprise.javacodegeeks.com/", "WebServiceImplService");

        Service service = Service.create(wsdlUrl, qname);

        WebServiceInterface helloWorldInterface = service.getPort(WebServiceInterface.class);

        System.out.println(helloWorldInterface.getHelloWorldAsString("- This is Java Code Geeks")); 
   } 
}

The output of the above program will be:

Hello World JAX-WS - This is Java Code Geeks

3. Java Web Service Client using wsimport tool

wsimport is command line utility that parses a deployed wsdl file and automatically creates the necessary java files that you will need to create your client program that uses the Web Service (most IDE’s use wsimport to generate these files automatically…)

wsimport is located to your JDK_PATH/bin folder. To parse the wsdl and generate the files I issued the following command :

wsimport.exe -keep http://localhost:8888/webserive/helloworld?wsdl -d F:\nikos7\Desktop
  • -keep : is an options that lets you keep the generated files
  • -d : you can sepcify the folder where the generated files will be stored

This is an image form the terminal that I used:

wsimport-command

This generated one interface and one service implementation file (stubs basically). We are going to use these files in order to create our client program.

WebServiceInterface,java:

package com.javacodegeeks.enterprise.ws;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Action;

/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.4-b01
 * Generated source version: 2.2
 * 
 */

@WebService(name = "WebServiceInterface", targetNamespace = "http://ws.enterprise.javacodegeeks.com/")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface WebServiceInterface {

    /**
     * 
     * @param arg0
     * @return
     *     returns java.lang.String
     */
    @WebMethod
    @WebResult(partName = "return")
    @Action(input = "http://ws.enterprise.javacodegeeks.com/WebServiceInterface/getHelloWorldAsStringRequest", output = "http://ws.enterprise.javacodegeeks.com/WebServiceInterface/getHelloWorldAsStringResponse")
   public String getHelloWorldAsString(
        @WebParam(name = "arg0", partName = "arg0")
        String arg0);

}

WebServiceImplService.java:

package com.javacodegeeks.enterprise.ws;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;

/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.4-b01
 * Generated source version: 2.2
 * 
 */
@WebServiceClient(name = "WebServiceImplService", targetNamespace = "http://ws.enterprise.javacodegeeks.com/", wsdlLocation = "http://localhost:8888/webserive/helloworld?wsdl")
public class WebServiceImplService
    extends Service
{

    private final static URL WEBSERVICEIMPLSERVICE_WSDL_LOCATION;
    private final static WebServiceException WEBSERVICEIMPLSERVICE_EXCEPTION;
    private final static QName WEBSERVICEIMPLSERVICE_QNAME = new QName("http://ws.enterprise.javacodegeeks.com/", "WebServiceImplService");

    static {
        URL url = null;
        WebServiceException e = null;
        try {
            url = new URL("http://localhost:8888/webserive/helloworld?wsdl");
        } catch (MalformedURLException ex) {
            e = new WebServiceException(ex);
        }
        WEBSERVICEIMPLSERVICE_WSDL_LOCATION = url;
        WEBSERVICEIMPLSERVICE_EXCEPTION = e;
    }

    public WebServiceImplService() {
        super(__getWsdlLocation(), WEBSERVICEIMPLSERVICE_QNAME);
    }

    public WebServiceImplService(WebServiceFeature... features) {
        super(__getWsdlLocation(), WEBSERVICEIMPLSERVICE_QNAME, features);
    }

    public WebServiceImplService(URL wsdlLocation) {
        super(wsdlLocation, WEBSERVICEIMPLSERVICE_QNAME);
    }

    public WebServiceImplService(URL wsdlLocation, WebServiceFeature... features) {
        super(wsdlLocation, WEBSERVICEIMPLSERVICE_QNAME, features);
    }

    public WebServiceImplService(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public WebServiceImplService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
        super(wsdlLocation, serviceName, features);
    }

    /**
     * 
     * @return
     *     returns WebServiceInterface
     */
    @WebEndpoint(name = "WebServiceImplPort")
    public WebServiceInterface getWebServiceImplPort() {
        return super.getPort(new QName("http://ws.enterprise.javacodegeeks.com/", "WebServiceImplPort"), WebServiceInterface.class);
    }

    /**
     * 
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the features parameter will have their default values.
     * @return
     *     returns WebServiceInterface
     */
    @WebEndpoint(name = "WebServiceImplPort")
    public WebServiceInterface getWebServiceImplPort(WebServiceFeature... features) {
        return super.getPort(new QName("http://ws.enterprise.javacodegeeks.com/", "WebServiceImplPort"), WebServiceInterface.class, features);
    }

    private static URL __getWsdlLocation() {
        if (WEBSERVICEIMPLSERVICE_EXCEPTION!= null) {
            throw WEBSERVICEIMPLSERVICE_EXCEPTION;
        }
        return WEBSERVICEIMPLSERVICE_WSDL_LOCATION;
    }

}

Now we simply create a client based on the above files :

WSClient.java:

package com.javacodegeeks.enterprise.ws;

import com.javacodegeeks.enterprise.ws.WebServiceImplService;

public class WSClient {
	public static void main(String[] args) {

	WebServiceImplService webService = new WebServiceImplService();
	WebServiceInterface serviceInterface = webService.getWebServiceImplPort();

	System.out.println(serviceInterface.getHelloWorldAsString("- This is Java Code Geeks"));
 }
}

The output of the above program will be:

Hello World JAX-WS - This is Java Code Geeks

This was a Hello World example on JAX-WS RPC Style Web Services. Download the Eclipse Projects of this tutorial: JAX-WS-HelloWorld.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close