1.什么是webservice:
服务端整出一些资源让客户端访问(获取数据)
一个跨语言、跨平台的规范
2.作用:跨平台调用、跨语言调用、远程调用
3.什么时候使用webservice:
1.新旧系统之间
2.不同公司之间:如淘宝与物流的数据交互
3.一些提供数据的应用:如天气预报,股票行情,手机号码归属地
4.重要术语
wsdl : web service definition language --网络服务定义语言
一个webservice 对应一个 wsdl 文档
soap :simple object access protocal 简单对象访问协议
基于http 和xml 的协议,用于web 上交换结构化,包含请求和响应
SEI :WebService EndPoint Interface(终端接口)
实现的方法逻辑,使用jdk里面的注解
5. 实现服务端的功能(SEI)
(1)创建接口,对接口及方法进行注解
package com.webservice.service; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public interface Hello { @WebMethod String sayHello(String name); }
(2)实现接口,类需要注解
package com.webservice.service; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public class HelloImpl implements Hello { public String sayHello(String name) { String result="say:"+name; System.out.println(result); return result; } }
(3)发布接口
package com.webservice.service; import javax.xml.ws.Endpoint; public class IssHello { public static void main(String []args){ String address="http://192.168.1.103:8080/connection/Hello"; Endpoint.publish(address,new HelloImpl()); System.out.println("成功发布——————"); } }
6.实现客户端的功能(调用服务端的接口,获取返回的参数)
(1)新建一个java项目
(2)借助jdk 的wsimport.exe 工具生成客户端的代码
在新建的java项目路径下,使用cmd 输入 wsimport -keep url
url 对应上面的address并在后面加上?wsdl 即:http://192.168.1.103:8080/connection/Hello?wsdl
运行后会在项目中生成一系列的文件
创建方法调用服务端代码
package com.webservice.service.test; import com.webservice.service.HelloImpl; import com.webservice.service.HelloImplService; public class Client { public static void main(String[] args) { HelloImplService factory=new HelloImplService(); // hello是一个代理对象 HelloImpl hello=factory.getHelloImplPort(); System.out.println(hello.getClass()); System.out.println(hello.sayHello("成功")); } }
end