JAX-WS Hello world

Categories: Java; Tagged with: ; @ July 19th, 2012 15:57

Create the service


package com.liguoliang.soap.jaxws;

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

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

	String getHelloWorldAsString(String name);


package com.liguoliang.soap.jaxws;

import javax.jws.WebService;

@WebService(endpointInterface = "com.liguoliang.soap.jaxws.IHelloWorld")
public class HelloWorldImpl implements IHelloWorld {

	public String getHelloWorldAsString(String name) {
		return "Hello: " + name;


Publish the service

package com.liguoliang.soap.jaxws;

import javax.xml.ws.Endpoint;

public class HelloWorldPublisher {

	public static void main(String[] args) {
		Endpoint.publish("http://localhost:8080/ws/hello", new HelloWorldImpl());

Use the Service from client

package com.liguoliang.soap.client;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import com.liguoliang.soap.jaxws.IHelloWorld;

public class HelloWorldClient {

	public static void main(String[] args) throws MalformedURLException {
		URL url = new URL("http://localhost:8080/ws/hello?wsdl");

		QName qName = new QName("http://jaxws.soap.liguoliang.com/",
		Service service = Service.create(url, qName);

		// Get the instance.
		IHelloWorld iheHelloWorld = service.getPort(IHelloWorld.class);

The outupt:
Hello: liguoliang.com


// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.