A simple JAX-WS service

原文地址:http://cxf.apache.org/docs/a-simple-jax-ws-service.html 

有修改去掉User

 

WebService 接口 HelloWorld:

package dm.com.service;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService														//必须
public interface HelloWorld {

	String sayHi(@WebParam(name = "name") String name);			//最好设置WebParam

	String saySorry(@WebParam(name = "name") String name);		//否则默认为arg0
}
 

接口实现HelloWorldImpl:

package dm.com.serviceimpl;

import javax.jws.WebService;

import dm.com.service.HelloWorld;

@WebService(endpointInterface = "dm.com.service.HelloWorld", serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {

	public String sayHi(String name) {
		// TODO Auto-generated method stub
		System.out.println("sayHi called");
		return "Hello, " + name;
	}

	public String saySorry(String name) {
		// TODO Auto-generated method stub
		System.out.println("saySorry called");
		return "Sorry, " + name;
	}

}
 

服务发布类ServicePublish:

package dm.com.publish;

import javax.xml.ws.Endpoint;

import dm.com.serviceimpl.HelloWorldImpl;

public class ServicePublish {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Starting Server");
		HelloWorldImpl implementor = new HelloWorldImpl();
		String address = "http://localhost:9090/HelloWorld";
		Endpoint.publish(address, implementor);
	}

}
 

浏览器访问:http://localhost:9090/HelloWorld?wsdl

 

浏览器调用:http://localhost:9090/HelloWorld/sayHi/name/wangwei

 

代码调用:

package dm.com.client;

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

import dm.com.service.HelloWorld;

public class ClientTest {

	private static final QName SERVICE_NAME = new QName(
			"http://service.com.dm/", "HelloWorld");
	private static final QName PORT_NAME = new QName("http://service.com.dm/",
			"HelloWorldPort");
	//不明白为什么不是HelloWorldImplPort  ????

	public static void main(String[] args) {
		Service service = Service.create(SERVICE_NAME);
		String endpointAddress = "http://localhost:9090/HelloWorld";
		service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING,
				endpointAddress);

		HelloWorld hw = service.getPort(HelloWorld.class);
		System.out.println(hw.sayHi("wangwei"));

	}

}
 

你可能感兴趣的:(apache,xml,webservice,浏览器,SOAP)