1.介绍
webservice主要解决两个系统两个应用程序间的远程调用,它提供了webapi方式访问,跨语言跨平台
2.原理
webservice的客户端与服务端进行交互的时候使用xml来传递数据,
soap协议,即简单对象访问协议,它是webservice的客户端与服务端进行交互的时候遵守的一个协议
3.方式
通过jdk自带工具wsimport,根据服务说明书生成本地java代码,之后通过这个本地代码访问webservice
wsimport -s . -p webservice http://127.0.0.1:8080/helloService?wsdl
webservice:java文件下载到本地的文件夹位置(相对cmd命令位置)
/helloService:提供调用的类文件名
4.例子
1)服务端
1.1 IMyServer
package test;import javax.jws.WebService;@WebServicepublic interface IMyServer { ????public int add(int a,int b); ????public int minus(int a,int b);}
1.2 MyServerImpl 实现类
package test;import javax.jws.WebService;@WebService(endpointInterface="test.IMyServer")public class MyServerImpl implements IMyServer{ ???@Override ???public int add(int a, int b) { ???????System.out.println(a+"+"+b+"="+(a+b)); ???????return a+b; ???} ???@Override ???public int minus(int a, int b) { ???????System.out.println(a+"-"+b+"="+(a-b)); ???????return a-b; ???}}
3.主程序发布代码
package test;import javax.xml.ws.Endpoint;public class test { ???public static void main(String[] args){ ???????String address = "http://localhost:9999/ns"; ???????Endpoint.publish(address, new MyServerImpl()); ???}}
2)客户端
先使用wximport方式下载分享类及其关联类,在将java文件都复制到新客户端项目中
调用
import testwebservice.IMyServer;import testwebservice.MyServerImplService;public class application { ???public static void main(String[] args){ ???????IMyServer myserver =new MyServerImplService().getMyServerImplPort(); ???????Integer result = myserver.add(2, 2); ???????System.out.println(result); ???}}
5. 网络上很多wsdl资源可以获取别人资源
http://www.webxml.com.cn/zh_cn/web_services.aspx
获得手机归属地webservce:
wsimport -s . -p lenve.test.phone http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
WebService
原文地址:http://www.cnblogs.com/xiaoping1993/p/webservice.html