Webservice是SOAP+XML,SOAP是基于Http的,Http底层是Socket,先回顾一下Socket:
Server:
public class Server { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(6789); while(true){ Socket s = ss.accept(); InputStream is = s.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String str = br.readLine(); System.err.println("收到客户端消息:"+str); br.close(); is.close(); s.close(); } } }
Client:
public class Client { public static void main(String[] args) throws Exception { Socket s = new Socket("172.28.12.128",6789); OutputStream os = s.getOutputStream(); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os)); bw.write("吃饭了某"); bw.close(); os.close(); s.close(); } }
++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
WebService用的是SOAP协议:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
使用jdk发布第一个WebService服务并调用
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1,发布代码,新建java项目WS_01
package com.lhy.ws; import javax.jws.WebService; import javax.xml.ws.Endpoint; /** * @WebService:将 Java 类标记为实现 Web Service,或者将 Java 接口标记为定义 Web Service 接口。 */ @WebService public class HelloService { public String sayHello(String name){ return "Hello "+name; } public String sayByeBye(String name){ return "Bye bye "+name; } public static void main(String[] args) { /** * 参数1:服务的发布地址 * 参数2:服务的实现者 * publish 会新启动一个线程 */ Endpoint.publish("http://127.0.0.1:8889/hello", new HelloService()); System.err.println("publish是新开的线程监听客户端请求,所以这里能够输出"); } }
2,查看WSDL:WSDL-说明服务在哪里,如何调用,其实就是一个使用说明书
浏览器输入 http://127.0.0.1:8889/hello?wsdl
3,调用,wsimport命令生成客户端代码
这个命令为什么能执行?和java、javac一样,jdk/bin目录下有这个命令,只要配置了环境变量就能执行
找一个目录E:杂项WebServiceclient_code 在这里生成客户端代码
-s . 指定代码生成在当前目录,也可以自定义包名,加上参数 -p com.xxxx
生成的代码:
新建java项目,把客户端.java代码copy到项目
测试类APP:
package com.lhy.ws; /** * 通过wsimport生成客户端代码调用WebService服务 */ public class App { public static void main(String[] args) { /** * WSDL: * <service name="HelloServiceService"> */ HelloServiceService hss = new HelloServiceService(); /** * WSDL: * <port name="HelloServicePort" binding="tns:HelloServicePortBinding"> */ HelloService hs = hss.getHelloServicePort(); String result = hs.sayHello("NB"); System.err.println(result); //com.sun.proxy.$Proxy32 JDK动态代理生成的代理类 System.err.println(hs.getClass().getName()); } }
http://www.webxml.com.cn 这个网站上有一些免费的服务可以调用,下边调用一下手机卡信息的服务
wsimport生成客户端代码
照着wsdl写代码:
public class MobileApp { public static void main(String[] args) { MobileCodeWS mobileCodeWS = new MobileCodeWS(); MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getMobileCodeWSSoap(); String result = mobileCodeWSSoap.getMobileCodeInfo("xxxxxxxx", ""); System.err.println(result); } }