(二)使用cxf框架开发webservice

一、 服务器端开发

1. 创建服务器工程,导入cxf的jar包
首先建立一个server的工程,在工程目录下新建一个lib目录,把apache-cxf框架下lib包下的jar包拷到server工程目录下的lib目录下,并导入到工程的环境中。

2. 新建服务器端响应接口

package com.lzj.webservice.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface Webservice {   
    @WebMethod
    public String sayHello(String name);
}

3. 新建服务器端响应接口的实现类

package com.lzj.webservice.ws;
import javax.jws.WebService;

@WebService
public class WebserviceImpl implements Webservice {

    @Override
    public String sayHello(String name) {
        System.out.println("hello:" + name);
        return "hello " + name;
    }

}

4. 发布服务器端服务

package com.lzj.webservice.ws.test;
import javax.xml.ws.Endpoint;
import com.lzj.webservice.ws.WebserviceImpl;

public class WebservicePublish {

    public static void main(String[] args) {

        String address = "http://localhost:8989/Webservice-server/webservice";
        Endpoint.publish(address, new WebserviceImpl());
        System.out.println("发布webservice成功");
    }

}

二、客户端开发

1.用cxf框架生成客户端程序
首先在系统的path环境中加入apache-cxf框架下的bin目录,以便在cmd中可以直接用bin目录下的命令。
新建一个webservice的客户端工程,然后在cmd中进入该工程的src目录下,然后用wsld2java命令进行生成客户端代码
wsdl2java http://localhost:8989/Webservice-server/webservice?wsd
生成代码如下:
这里写图片描述
注意:在用wsdl命令生成代码之前,一定要先运行服务器端的发布服务程序,否则会报一个网略连接的错误。

2.编写测试代码,获取服务器端应用

package com.lzj.webservice.ws.client;
import com.lzj.webservice.ws.Webservice;
import com.lzj.webservice.ws.WebserviceImplService;

public class WsClient {

    public static void main(String[] args) {
        WebserviceImplService factory = new WebserviceImplService();
        Webservice ws = factory.getWebserviceImplPort();
        String result = ws.sayHello("lzj");
        System.out.println("result : " + result);
    }

}

运行完服务器端程序后,然后运行上述客户端的测试程序,客户端输出结果如下:
result : hello lzj
说明已获取到服务器端的服务。

三、WSDL文档

当运行完服务器端的发布程序后,可以在浏览器中输入 http://localhost:8989/Webservice-server/webservice?wsd 连接查看服务端与客户端通讯的wsdl文档格式,内容如下

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"xmlns:tns="http://ws.webservice.lzj.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="WebserviceImplService" targetNamespace="http://ws.webservice.lzj.com/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ws.webservice.lzj.com/" elementFormDefault="unqualified"targetNamespace="http://ws.webservice.lzj.com/" version="1.0">
<xs:element name="sayHello" type="tns:sayHello"/>
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xs:complexType name="sayHello">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayHelloResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="Webservice">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello"></wsdl:input>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="WebserviceImplServiceSoapBinding" type="tns:Webservice">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="WebserviceImplService">
<wsdl:port binding="tns:WebserviceImplServiceSoapBinding" name="WebserviceImplPort">
<soap:address location="http://localhost:8989/Webservice-server/webservice"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

上面的内容对应的标签如下:

<definitions>
        <types>
            <schema>
                <element>
        </types>
        <message>
            <part>
        </message>
        <portType>
            <operation>
                <input>
                <output>
        </portType>
        <binding>
            <operation>
                <input>
                <output>
        </binding>
        <service>
            <port>
                <address>
        </service>
</definitions>

标签的定义:
• types - 数据类型(标签)定义的容器,里面使用schema定义了一些标签结构供message引用
• message - 通信消息的数据结构的抽象类型化定义。引用types中定义的标签
• operation - 对服务中所支持的操作的抽象描述,一个operation描述了一个访问入口的请求消息与响应消息对。
• portType - 对于某个访问入口点类型所支持的操作的抽象集合,这些操作可以由一个或多个服务访问点来支持。
• binding - 特定端口类型的具体协议和数据格式规范的绑定。
• service- 相关服务访问点的集合
• port - 定义为协议/数据格式绑定与具体Web访问地址组合的单个服务访问点。

标签之间的引用关系如下:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值