webservice学习笔记
webservice概述
webservice简单来说就是http+xml。http说的是webservice是基于web的服务,在服务端提供一些资源让客户端去访问;xml指的是webservice接口的请求和响应数据格式,使用xml数据格式解决不同语言之间的通信问题。
图1-1是webservice的调用流程图,从图中我们可以看出服务端的webservice相当于MVC中的controller控制器,接受和返回xml数据,屏蔽不同语言之间的差异。
(图1-1)
开发webservice客户端和服务端
使用JDK开发webservice
1)开发服务端
开发服务端需要1.6.0版本以上的JDK。分为两个步骤,定义service和发布service。代码如下所示:
service接口
package cn.lzz.ws.api;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService(serviceName="CallSomeoneService")
public interface ICallSomeone {
@WebMethod
String callSomeone(String name);
}
service实现类
package cn.lzz.ws.impl;
import javax.jws.WebService;
import cn.lzz.ws.api.ICallSomeone;
@WebService(serviceName="CallSomeoneService")
public class CallSomeoneImpl implements ICallSomeone{
@Override
public String callSomeone(String name) {
System.out.println("Hello,"+name);
return "success";
}
}
发布webservice
package cn.lzz.ws.publish;
import javax.xml.ws.Endpoint;
import cn.lzz.ws.impl.CallSomeoneImpl;
public class PublishWS {
public static void main(String[] args) {
//webservice地址
String address="http://localhost:8280/callSomeone";
Endpoint.publish(address, new CallSomeoneImpl());
System.out.println("发布完成。。。。。");
}
}
2)开发Client端
首先,查看Webservice接口的wsdl文档,直接在webservice地址后加?wsdl访问与之对应的wsdl。比如访问上文发布的webservice的wsdl文档,http://localhost:8280/callSomeone?wsdl。
其次,借助JDK的wsimport.exe工具生成客户端的webservice代码。wsimport命令使用格式:wsimport -keep url 。url表示wsdl文档地址。如果没有加-keep选项,生成的客户端代码只有.class文件,没有.java文件。打开项目的src目录,调用wsimport命令,自动生成客户端代码,如图2-1所示:
(2-1)
最后,借助wsimport工具生成的代码,调用webservice接口请求数据。详细代码如下所示:
package cn.lzz.ws.client;
import cn.lzz.ws.impl.CallSomeoneImpl;
import cn.lzz.ws.impl.CallSomeoneService;
public class CallWSTest {
public static void main(String[] args) {
//获取代理Service
CallSomeoneService service=new CallSomeoneService();
//获取实现类
CallSomeoneImpl callSomeoneImpl = service.getCallSomeoneImplPort();
//调用webservice
callSomeoneImpl.callSomeone("阿蛮");
}
}
CallSomeoneService的名称和服务端的@WebService注解的serviceName属性相对应,有了代理Service之后,还需获取相应的终端实现类才能调用webservice接口。
webservice中的几个重要概念
看了上面的介绍和代码,想必大家对webservice还是比较陌生,下面以上文的wsdl文档为参照,对webservice中的几个重要概念做进一步的阐述,方便我们理解。
Schema约束
1.namespace,相当于schema文件的id;
2.targetNamespace属性,用来指定schema文件的namespace的值;
3.xmlns属性,引入一个约束, 它的值是一个schema文件的namespace值;
4.schemaLocation属性,用来指定引入的schema文件的位置。
xml文档中的所有的标签和属性,都需要在schema文件定义,可以通过xmlns属性引入schema约束文件,引入的约束文件需要起别名如xmlns:xsd,xsd就是别名。
wsdl
web service definition language,简单来说就是webservice定义语言,定义了webservice的服务端和客户端交互的请求和响应的数据格式和方式。一个webservice对应一个唯一的wsdl。wsdl的作用可以参照图3-1来理解。
(3-1)
上文的webservice接口对应的wsdl文件如下所示:
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.ws.lzz.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://impl.ws.lzz.cn/" name="CallSomeoneService">
<!--定义当前wsdl文档中所使用的标签,xml片断所包含的一些标签-->
<types>
<xs:schema xmlns:tns="http://impl.ws.lzz.cn/" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://impl.ws.lzz.cn/">
<xs:element name="callSomeone" type="tns:callSomeone"/>
<xs:element name="callSomeoneResponse" type="tns:callSomeoneResponse"/>
<xs:complexType name="callSomeone">
<xs:sequence>
<xs:element name="arg0" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="callSomeoneResponse">
<xs:sequence>
<xs:element name="return" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</types>
<!--定义请求和响应消息,name属性指定消息的名称,part name属性指定消息的组成,依赖<types>中定义的某个element -->
<message name="callSomeone">
<part name="parameters" element="tns:callSomeone"/>
</message>
<message name="callSomeoneResponse">
<part name="parameters" element="tns:callSomeoneResponse"/>
</message>
<!--
定义SEI接口
portType name 指定SEI接口名
operation 接口的操作,也就是其方法
input message 服务器端接收的消息,依赖指定的<message>,也就是将哪个消息作为请求消息对应方法的参数
output message 指定服务器返回的消息,依赖指定的<message>,也就是将哪个消息作为响应消息对应方法的返回值
-->
<portType name="CallSomeoneImpl">
<operation name="callSomeone">
<input wsam:Action="http://impl.ws.lzz.cn/CallSomeoneImpl/callSomeoneRequest" message="tns:callSomeone"/>
<output wsam:Action="http://impl.ws.lzz.cn/CallSomeoneImpl/callSomeoneResponse" message="tns:callSomeoneResponse"/>
</operation>
</portType>
<!--
定义服务器端处理请求的SEI实现类对象
binding type :参照<portType>所定义的SEI
soap:binding : 绑定的方式,也就数据传递的方式 为文档(即xml)
input : 指定请求消息(与<portType>中的input对应)
output:指定响应消息(与<portType>中的output对应)
body use="literal" 消息体为文本
-->
<binding name="CallSomeoneImplPortBinding" type="tns:CallSomeoneImpl">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="callSomeone">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<!--
定义客户端调用web service的入口
service name :生产客户端的SEI接口实现的工厂类
port binding :处理请求的服务器端的SEI接口实现类对象
address location :web service的请求url
-->
<service name="CallSomeoneService">
<port name="CallSomeoneImplPort" binding="tns:CallSomeoneImplPortBinding">
<soap:address location="http://localhost:8280/callSomeone"/>
</port>
</service>
</definitions>
SEI
web service endpoint interface,webservice的终端接口,服务端用来处理某个请求的接口。
SAOP
simple object access protocal,由http+xml片断组成的请求消息和响应消息,依赖于wsdl文档的定义。