JAX-WS WebService

一、简介

JAX-WS规范是一组XML web services的JAVA API,以下示例是使用jdk提供的接口实现一个简单的web service。

二、示例

1、使用接口服务器端

建一个web工程,分别生命建一个接口和一个实现类,并加上响应的webservice注解,代码如下

//接口
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface SayHiService {
	@WebMethod
	String sayHi(@WebParam(name = "name") String name);
}

//实现
import javax.jws.WebService;

@WebService(endpointInterface = "com.test.ws.SayHiService",serviceName="SayHiService")
public class SayHiServiceImp implements SayHiService {
	// 访问地址:http://127.0.0.1:8080/webSrv/SayHiService?wsdl
	@Override
	public String sayHi(String name) {
		System.out.println(name + ",Hi World!");
		return name + ",Hi World!";
	}

}

2、不使用接口的服务器端

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

@WebService(serviceName = "SayHelloService")
public class SayHelloService {
	@WebMethod(operationName = "sayHi")
	public String sayHi(@WebParam(name = "name") String name) {
		System.out.println(name + ",Hello World!");
		return name + ",Hello World!";
	}
}

3、通过jdk指令生成客户端代码

注意:此时服务器端的web应用必须是已经发的的

wsimport -keep http://127.0.0.1:8080/webSrv/SayHelloService?wsdl -d D:\temp\ -p com.test.wsc.hello

4、写客户端代码

把生成的代码复制到新建的java工程中的对应package下,并新建一个测试类进行测试,测试类代码如下

import com.test.wsc.SayHiService;
import com.test.wsc.SayHiService_Service;

public class WSTest {

	public static void main(String[] args) {
		SayHiService_Service servie = new SayHiService_Service();
		SayHiService shs = servie.getSayHiServiceImpPort();
		String sayHi = shs.sayHi("张三");
		System.out.println(sayHi);
	}

}


你可能感兴趣的:(Web,service,jax-ws)