1.是什么:
JAX-WS 规范是一组XML web services的JAVA API,它运行时实现会将这些API的调用转换成为对应的SOAP消息,是Sun公司提出的一套关于WebService的开发标准。
2.能干嘛:
JAX-WS 可以完成wsdl到java的转换,即wsdl或合同契约优先.
服务端开发完成后客户端如何编码调用,全手写麻烦否,那接下来就可以通过JAX-WS全自动完成。
3.怎么玩:
(1) 在自己的服务器端生成cxf_server.wsdl文件
(2) 到自己apache所在jar包的bin目录下输入:wsdl2java -client 服务器端口地址后面在加?wsdl。
(3) 将客户端用不到的类和代码删除;
(4) 为了工程解耦,将客户端HelloWorld和HelloWorldImplService进行压缩为jar包放在lib目录下。
(5) 在客户端HelloWorld_HelloWorldImplPort_Client类中,将多余的代码删除,输入参数进行调用。
/**
* Please modify this class to meet your needs
* This class is not complete
*/
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
/**
* This class was generated by Apache CXF 3.1.5
* 2017-05-27T22:50:14.474+08:00
* Generated source version: 3.1.5
*
*/
public final class HelloWorld_HelloWorldImplPort_Client {
private static final QName SERVICE_NAME = new QName("http://test.cfx.atguigu.com/", "HelloWorldImplService");
private HelloWorld_HelloWorldImplPort_Client() {
}
public static void main(String args[]) throws java.lang.Exception {
URL wsdlURL = HelloWorldImplService.WSDL_LOCATION;
if (args.length > 0 && args[0] != null && !"".equals(args[0])) {
File wsdlFile = new File(args[0]);
try {
if (wsdlFile.exists()) {
wsdlURL = wsdlFile.toURI().toURL();
} else {
wsdlURL = new URL(args[0]);
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
HelloWorldImplService ss = new HelloWorldImplService(wsdlURL, SERVICE_NAME);
HelloWorld port = ss.getHelloWorldImplPort();
System.out.println(port.sayHello("Tom",18));
System.exit(0);
}
}
说明:如果还有别人调用我们的代码,那我们通过如下方式生成一份接口文档供别人调用即可,方式如下:
选中HelloWorld和HelloWorldImplService点击如下:
最终生成的文档结构图如下:
PS:若想结合源码更好的理解WebService,请:https://github.com/luomingkui/WebService