cxf实战一

来源:互联网 发布:mac搜索移动硬盘文件 编辑:程序博客网 时间:2024/05/20 20:58

Apache CXF提供了用于方便地构建和开发WebService的可靠基础架构。它允许创建高性能和可扩展的服务,可以部署在Tomcat和基于Spring的轻量级容器中,也可以部署在更高级的服务器上,例如Jboss、WebSphere或WebLogic。 
CXF提供了以下功能:

  • WebService服务标准支持:

    • Java API for XML Web Services (JAX-WS)
    • SOAP
    • WebService描述语言(Web Services Description Language ,WSDL)
    • 消息传输优化机制(Message Transmission Optimization Mechanism,MTOM)
    • WS-Basic Profile
    • WS-Addressing
    • WS-Policy
    • WS-ReliableMessaging
    • WS-Security
  • 前端建模:CXF允许使用不同的前端API来创建Service。如CXF允许使用简单的工厂Bean并通过JAX-WS实现来创建WebService,允许创建动态WebService客户端。

  • 工具支持:CXF提供了在Java Bean、WebService和WSDL之间进行转换的工具,提供了对Maven和Ant集成的支持,并无缝地支持Spring集成。
  • RESTful支持:CXF支持Restful,并支持Java平台的JAX-RS实现。
  • 对不同传输和绑定的支持:CXF支持不同数据类型的传输,除了支持SOAP和HTTP协议绑定外,还支持JAXB和AEGIS绑定。
  • 对非XML绑定的支持:CXF支持非XML绑定,如JSON、CORBA、JBI和SCA等。
  • Code First和Xml First:CXF支持使用Code First或者Xml First的方式创建WebService。

下面用CXF创建独立发布的WebService。

服务端

在Eclipse中创建maven项目,配置文件pom.xml中引入CXF,具体配置如下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.rvho</groupId>    <artifactId>cxfstandalone</artifactId>    <version>0.0.1-SNAPSHOT</version>    <properties>        <!-- 文件拷贝编码 -->        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <!-- 输出编码 -->        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <!-- 编译编码 -->        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>        <!-- CXF版本 -->        <cxf.version>3.1.1</cxf.version>    </properties>    <dependencies>        <!-- CXF -->        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-frontend-jaxws</artifactId>            <version>${cxf.version}</version>        </dependency>        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-transports-http</artifactId>            <version>${cxf.version}</version>        </dependency>        <dependency>            <!-- 如果CXF不集成到Web服务器中,必须添加该引用 -->            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-transports-http-jetty</artifactId>            <version>${cxf.version}</version>        </dependency>        <!-- End CXF -->    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <version>3.3</version>                <configuration>                    <!-- 指定source和target的jdk版本是1.8 -->                    <source>1.8</source>                    <target>1.8</target>                </configuration>            </plugin>        </plugins>    </build></project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

服务接口HelloWS,该接口发布welcome方法。

package com.rvho.cxfstandalone.ws;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebService;@WebService(        name = "HelloWS",        targetNamespace = "http://www.tmp.com/services/hello"    )public interface HelloWS {    @WebMethod    String welcome(@WebParam(name = "name") String name);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

服务接口HelloWS实现类HelloWSImpl。

package com.rvho.cxfstandalone.ws.impl;import javax.jws.WebService;import com.rvho.cxfstandalone.ws.HelloWS;@WebService(        endpointInterface = "com.rvho.cxfserver.ws.HelloWS",        portName = "HelloWSPort",        serviceName = "HelloWSService",        targetNamespace = "http://www.tmp.com/services/hello"    )public class HelloWSImpl implements HelloWS {    @Override    public String welcome(String name) {        return "Welcome " + name;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

发布服务

package com.rvho.cxfstandalone;import org.apache.cxf.jaxws.JaxWsServerFactoryBean;import com.rvho.cxfstandalone.ws.HelloWS;import com.rvho.cxfstandalone.ws.impl.HelloWSImpl;public class CxfServer {    public static void main(String[] args) {        JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();        factory.setServiceClass(HelloWS.class);        //服务发布的地址        factory.setAddress("http://localhost:8999/services/hello");        factory.setServiceBean(new HelloWSImpl());        factory.create();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

在浏览器中输入服务发布地址http://localhost:8999/services/hello?wsdl如果看到如下内容,表示服务发布成功。 
这里写图片描述

客户端

用wsimport或者wsdl2java把服务端发布的服务生成java对象。 
wsimport是jdk提供的wsdl转java对象工具,在jdk的bin目录下可以找到。用法是

"D:\Program Files\Java\jdk1.8.0_25\bin\wsimport.exe" -encoding utf-8 -d D:\dev\wsdl\cxfserver\wsimport\compile -s D:\dev\wsdl\cxfserver\wsimport\src -p com.rvho.cxfclient.wsdl.wsimport.hello http://localhost:8999/services/hello?wsdl
  • 1

wsdl2java是cxf提供的wsdl转java对象工具,在cxf的bin目录下可以找到。用法是

"D:\dev\library\java\cxf\apache-cxf-3.1.0\bin\wsdl2java" -p com.rvho.cxfclient.wsdl.cxf.hello -d D:\dev\wsdl\cxfserver\cxf\src http://localhost:8999/services/hello?wsdl
  • 1

上面命令的路径是我本机路径,有可能不一样。 
客户端可以通过jaxws调用,也可以通过cxf调用。

//jaxws调用//URL不是必须的,除非服务的地址有改变URL wsdlUrl = new URL("http://localhost:8999/services/hello?wsdl");HelloWSService helloWSS = new HelloWSService(wsdlUrl);HelloWS helloWS = helloWSS.getHelloWSPort();String welcome = helloWS.welcome("accountwcx@qq.com");System.out.println(welcome);/*    cxf调用    首先在客户端添加cxf    <dependency>        <groupId>org.apache.cxf</groupId>        <artifactId>cxf-rt-frontend-jaxws</artifactId>        <version>3.1.1</version>    </dependency>    <dependency>        <groupId>org.apache.cxf</groupId>        <artifactId>cxf-rt-transports-http</artifactId>        <version>3.1.1</version>    </dependency>*/JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();factory.setServiceClass(HelloWS.class);factory.setAddress("http://localhost:8999/services/hello");HelloWS helloWS = factory.create(HelloWS.class);String welcome = helloWS.welcome("accountwcx@qq.com");System.out.println(welcome);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
原创粉丝点击