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"));

	}

}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值