JAX-WS Hello world

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

Create the service

Interface

package com.liguoliang.soap.jaxws;

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 IHelloWorld {

	@WebMethod
	String getHelloWorldAsString(String name);
}

Implement:

package com.liguoliang.soap.jaxws;

import javax.jws.WebService;

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

	@Override
	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/",
				"HelloWorldImplService");
		Service service = Service.create(url, qName);

		// Get the instance.
		IHelloWorld iheHelloWorld = service.getPort(IHelloWorld.class);
		System.out.println(iheHelloWorld.getHelloWorldAsString("liguoliang.com"));
	}
}

The outupt:
Hello: liguoliang.com



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