龙空技术网

webservice的学习

繁华殆尽时望一切安好 268

前言:

而今大家对“apachecxf文档”大概比较讲究,大家都需要学习一些“apachecxf文档”的相关文章。那么小编在网络上汇集了一些关于“apachecxf文档””的相关文章,希望兄弟们能喜欢,姐妹们快快来学习一下吧!

webservice是什么?

基于web的服务:服务器端整出一些资源让客户端应用访问(获取数据)一个跨平台、跨语言的规范(抽象)多个跨平台、跨平台的应用间通信整合的方案(实际)

为什么要用webservice

webservice能解决跨平台调用webservice能解决跨语言调用webservice能解决远程调用

什么时候用webservice

同一家公司的新旧应用之间的通信(语言不同)不同公司的应用之间(语言不同)一些提供数据的内容就会应用:天气预报、股票行情等

webservice的几个重要的术语

wsdl:webservice definition language(webservice定义语言)

对应一种类型的文件.wsdl

定义了webservice的服务器端与客户端应用交互传递请求和响应数据的格式和方式

一个webservice对应一个唯一的wsdl文档

soap:simple object access protocol(简单对象访问协议)

一种简单的、基于http和xml的协议,用于在web上交换结构化的数据

soap消息:请求消息和响应消息

http+xml片段

SEI:webservice endpoint interface(webservice终端接口)

webservice服务器端用来处理请求的接口

cxf:celtix+xfire

一个apache的用于开发webservice服务器端和客户端的框架

用jdk开发webservice

开发服务器端

webservice编码:@WebService(SEI的实现类);@WebMethod(SEI中的所有方法)

发布webservice

Endpoint(终端发布webservice)

示例代码

** * SEI接口 */@WebServicepublic interface WebSEI {    @WebMethod    public String sayHello(String name);}/** * SEI的实现类 */@WebServicepublic class WebSEIImpl implements WebSEI {    @Override    public String sayHello(String name) {        System.out.println("server say hello " + name);        return "Hello " + name;    }}/** * 发布webservice */public class WebEndpoint {    public static void main(String[] args) {        /**         * 参数说明:1.发布地址,2.接口实例         * 发布地址中IP是主机地址,其余可以自定义         */        Endpoint.publish(";, new WebSEIImpl());        System.out.println("发布成功!");    }}

上面的main方法启动之后,可以在浏览器输入进行访问和查看wsdl文档。

wsdl文档解析

文件格式大概如下:

<definitions>	<types>		<schema>			<element>  <message>    <part>  <portType>    <operation>      <input>      <output>	<binding>		<operation>			<input>			<output>	<service>
<types>    <xsd:schema>        <xsd:import namespace="; schemaLocation=";/>    </xsd:schema></types>

上面这段是定义webservice的wsdl文档中引入的约束,其中schemaLocation=";/>是约束内容的地址,可以用浏览器进行访问查看,当然这需要一定schema约束相关的知识,可以事先了解下。

<message name="sayHello">    <part name="parameters" element="tns:sayHello"/></message><message name="sayHelloResponse">    <part name="parameters" element="tns:sayHelloResponse"/></message>

上面的</message>是定义webservice所有的消息结构;

<part>标签通过element属性指定标签里schema中的约束中的片段。

<portType name="WebSEIImpl">    <operation name="sayHello">        <input wsam:Action="; message="tns:sayHello"/>        <output wsam:Action="; message="tns:sayHelloResponse"/>    </operation></portType>

上面的<portType>标签是用来定义服务器端的SEI;

<operation>标签用来指定一个webservice中的处理请求的方法;

<input>指定客户端应用传过来的数据,会引用上面定义的;

<output>指定服务器端返回给客户端的数据,会引用上面定义的;

<binding name="WebSEIImplPortBinding" type="tns:WebSEIImpl">    <soap:binding transport="; style="document"/>    <operation name="sayHello">    <soap:operation soapAction=""/>        <input>            <soap:body use="literal"/>        </input>        <output>            <soap:body use="literal"/>        </output>    </operation></binding>

<binding>用于定义SEI的实现类,type属性引用上面的;

<operation>用来定义方法与上面中的差不多,这里name属性是服务接口的方法名;

<soap:binding transport="; style="document"/>表示传输数据格式是文档(xml);

<soap:body use="literal"/>表示数据是文本数据;

<service name="WebSEIImplService">    <port name="WebSEIImplPort" binding="tns:WebSEIImplPortBinding">        <soap:address location=";/>    </port></service>

<service>是代表一个webservice容器,name属性指定webservice客户端的容器类

<port>代表一个服务器端SEI的一个实现,bingding属性引用上面的标签,name属性是指定webservice客户端真正的所需要的服务类;

<address>代表当前webservice的请求地址

开发客户端

查看对应的wsdl文档(一般浏览器 ....?wsdl)

请求webservice并查看请求和响应消息(一般浏览器)

创建客户端应用编码方式访问

借助jdk的wsimort.exe工具生成客户端代码:进入项目src目录下,打开cmd窗口,输入wsimport -keep url(url是wsdl文件的路径或者是wsdl文档)

生成的代码类如下:

借助生成的代码编写请求代码

public class ClientTest {    public static void main(String[] args) {        WebSEIImplService factory = new WebSEIImplService();        WebSEIImpl webSEI = factory.getWebSEIImplPort();        System.out.println(webSEI.sayHello("jack"));    }}

用cxf开发webservice

用cxf比jdk的优势

jdk不支持map与复杂的object类型,cxf支持所有类型

cxf提供很多拦截器,后期业务拓展性高

所需依赖:

<dependency>    <groupId>org.apache.cxf</groupId>    <artifactId>cxf-core</artifactId>    <version>3.4.4</version></dependency><dependency>    <groupId>org.apache.cxf</groupId>    <artifactId>cxf-rt-frontend-jaxws</artifactId>    <version>3.4.4</version></dependency><dependency>    <groupId>org.apache.cxf</groupId>    <artifactId>cxf-rt-transports-http-jetty</artifactId>    <version>3.4.4</version></dependency>
服务端代码示例:
  JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();//暴露地址factoryBean.setAddress(";);//SEI接口factoryBean.setServiceClass(WebSEI2.class);//SEI实现类factoryBean.setServiceBean(new WebSEI2Impl());factoryBean.create();System.out.println("发布成功!");
创建客户端应用编码方式访问

首先可以在下载cxf压缩包,并且配置环境变量;

然后进入项目src目录下,打开cmd窗口,输入wsdl2java url(url是wsdl文件的路径或者是wsdl文档)生成客户端所需要的代码

或者maven的pom文件添加

<plugin>    <groupId>org.apache.cxf</groupId>    <artifactId>cxf-codegen-plugin</artifactId>    <version>3.5.0</version>    <executions>        <execution>            <id>generate-sources</id>            <phase>generate-sources</phase>            <configuration>                <sourceRoot>generated/cxf</sourceRoot>                <wsdlOptions>                    <wsdlOption>                        <wsdl>;/wsdl>                    </wsdlOption>                </wsdlOptions>            </configuration>            <goals>                <goal>wsdl2java</goal>            </goals>        </execution>    </executions></plugin>

然后运行

mvn clean generate-sources

这将生成 wsdl 对应的客户端代码,代码会存放于 /generated/cxf 目录.

又或者直接写客户端代码:

{    JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();    Client client = clientFactory.createClient(";);    Object[] result = client.invoke("sayHappy", "KEVIN");    System.out.println(result[0]);}

所需依赖:

<dependency>    <groupId>org.apache.cxf</groupId>    <artifactId>cxf-core</artifactId>    <version>3.4.4</version></dependency><dependency>    <groupId>org.apache.cxf</groupId>    <artifactId>cxf-rt-frontend-jaxws</artifactId>    <version>3.4.4</version></dependency><dependency>    <groupId>org.apache.cxf</groupId>    <artifactId>cxf-rt-transports-http-jetty</artifactId>    <version>3.4.4</version></dependency>

标签: #apachecxf文档