zoukankan      html  css  js  c++  java
  • CXF使用教程(一)——简介以及入门案例

    一.简介

    Apache CXF是一个开源的Service框架,可以用于简化用户的service开发,基于CXF开发的应用可提供SOAP、XML/HTTP、RESTFUL HTTP或CORBA等服务。CXF底层页可以使用不同的传输协议,包括HTTP、JMS或JBI等。

    特性:

    •   支持大量的Web Service标准,包括SOAP、WS-I Basic Profile、WSDL、WS-Addressing、WS-Policy、WS-ReliableMessaging和WS-Security。
    • CXF支持大量的前端(frontend)编程模型。CXF实现了标准的JAX-WS API,它也包括一种被称为简单前端(simple frontend)的模型,这种模型无需annotation支持。CXF支持web service的两种开发模式:①规则(contract)优先的开发模式,即通过编写WSDL来开发web service;②代码优先的开发模式,即通过编写java代码来开发webservice.

    二.下载与安装(非maven)

    (1)登陆CXF官方站点:http://cxf.apache.org/,下载CXF最新版。笔者下载的为3.0.1版。
        (2)将下载得到的压缩包解压得到apache-cxf-3.0.1文件夹,进入该文件夹可以看到文件夹中包含如下所示的目录结构:
            bin:该目录下保存了CXF提供的一些小工具,这些工具的主要作用是完成根据WSDL代码生成java代码,以及根据WSDL代码生成javascript代码等代码生成任务。
            docs:该目录下有一个api子目录,其中保存了CXF的API文档。
            etc:该目录主要存放了CXF框架的一些杂项。
    lib:该目录存放了CXF的核心类库以及编译和运行所依赖的第三方类库。
    licenses:该目录下存放CXF以及第三方框架的授权文件。
    modules:该目录下存放CXF按模块打包的jar包。
    samples:该目录下存放了CXF的大量示例应用。这些应用是学习CXF极好的资料。
    license和readme等文档
    (3)将解压路径下的bin目录添加到系统的path环境变量,以便操作系统能找到bin目录下的命令,方便以后使用CXF提供的小工具。
    (4)为了在项目中使用CXF框架,需要将lib文件夹下的jar包添加到项目中。

    三.Web Service注释介绍
    webservice注释主要有:
    @WebService:定义服务,用在类上
    @WebMethod:定义方法,用于方法上
    @WebResult:定义返回值,用在方法上
    @WebParam:定义参数,用在方法上

    四.入门案例(maven方法)

    1.新建一个maven项目,在pom中添加依赖和jetty作为测试的web service的web容器

    <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>cn.outofmemory</groupId>
      <artifactId>hello-apache-cxf</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>hello-apache-cxf</name>
      <url>http://maven.apache.org</url>
    
        <properties>  
            <cxf.version>2.2.7</cxf.version>  
        </properties>  
    
        <dependencies>  
            <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>  
                <groupId>org.apache.cxf</groupId>  
                <artifactId>cxf-rt-transports-http-jetty</artifactId>  
                <version>${cxf.version}</version>  
            </dependency>  
            <dependency>  
                <groupId>org.apache.cxf</groupId>  
                <artifactId>cxf-rt-ws-security</artifactId>  
                <version>${cxf.version}</version>  
            </dependency>  
            <dependency>  
                <groupId>org.apache.cxf</groupId>  
                <artifactId>cxf-rt-ws-policy</artifactId>  
                <version>${cxf.version}</version>  
            </dependency>  
            <dependency>  
                <groupId>org.apache.cxf</groupId>  
                <artifactId>cxf-bundle-jaxrs</artifactId>  
                <version>${cxf.version}</version>  
            </dependency>  
            <dependency>  
                <groupId>javax.ws.rs</groupId>  
                <artifactId>jsr311-api</artifactId>  
                <version>1.1.1</version>  
            </dependency>  
            <dependency>  
                <groupId>org.slf4j</groupId>  
                <artifactId>slf4j-api</artifactId>  
                <version>1.5.8</version>  
            </dependency>  
            <dependency>  
                <groupId>org.slf4j</groupId>  
                <artifactId>slf4j-jdk14</artifactId>  
                <version>1.5.8</version>  
            </dependency>  
            <dependency>  
                <groupId>commons-httpclient</groupId>  
                <artifactId>commons-httpclient</artifactId>  
                <version>3.0</version>  
            </dependency>  
            <dependency>  
                <groupId>commons-io</groupId>  
                <artifactId>commons-io</artifactId>  
                <version>2.3</version>  
            </dependency>  
            <dependency>  
                <groupId>junit</groupId>  
                <artifactId>junit</artifactId>  
                <version>4.8.1</version>  
                <scope>test</scope>  
            </dependency>  
      </dependencies>
    
     <build>  
            <finalName>hello-apache-cxf</finalName>  
            <resources>  
                <resource>  
                    <directory>src/main/resources</directory>  
                </resource>  
                <resource>  
                    <directory>src/main/java</directory>  
                    <includes>  
                        <include>**</include>  
                    </includes>  
                    <excludes>  
                        <exclude>**/*.java</exclude>  
                    </excludes>  
                </resource>  
            </resources>  
            <plugins>  
                <plugin>  
                    <groupId>org.mortbay.jetty</groupId>  
                    <artifactId>maven-jetty-plugin</artifactId>  
                    <configuration>  
                        <contextPath>/</contextPath>  
                        <connectors>  
                            <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">  
                                <port>9000</port>  
                            </connector>  
                        </connectors>  
                    </configuration>  
                </plugin>  
                <plugin>  
                    <groupId>org.apache.maven.plugins</groupId>  
                    <artifactId>maven-compiler-plugin</artifactId>  
                    <configuration>  
                        <source>1.5</source>  
                        <target>1.5</target>  
                    </configuration>  
                </plugin>  
            </plugins>  
        </build>  
    </project>

    2.定义web service接口,在接口定义中要添加必要的annotation注解来标注出来webservice接口和提供的方法,以及参数等,如下接口文件:

    package cn.outofmemory.hello.apache.cxf;
    
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebResult;
    import javax.jws.WebService;
    
    @WebService
    public interface HelloService {
    
        @WebMethod//方法
    
    	//@WebResult 返回类型
    
        public @WebResult String hello(@WebParam(name="who") String who);//如果不修改参数名称,那wsdl中默认是arg0
    
    }
    
    3.实现接口:
    
    package cn.outofmemory.hello.apache.cxf;
    
    public class SimpleHelloService implements HelloService {
    
        public String hello(String who) {
            return "hello " + who;
        }
    
    }


    4.在web容器中运行web Service:

    package cn.outofmemory.hello.apache.cxf;
    
    import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
    
    public class Server {
    
        public static void main(String[] args) throws Exception {
            JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();  
            factory.setServiceClass(SimpleHelloService.class);  
    
            factory.setAddress("http://localhost:9000/ws/HelloService");  
            factory.create();  
    
            System.out.println("Server start...");  
        }
    }


    可以运行这个类,然后再浏览器中访问:http://localhost:9000/ws/HelloService?wsdl

    可以让Server端保持启动状态,下面我们写Client端来调用server端的webservice,如下client端代码:

    5.客户端代码如下:

    package cn.outofmemory.hello.apache.cxf;
    
    import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
    
    public class ServiceClient {
        public static void main(String[] args) {  
            JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
            factory.setServiceClass(HelloService.class);  
            factory.setAddress("http://localhost:9000/ws/HelloService");  
            HelloService helloworld = (HelloService) factory.create();  
            System.out.println(helloworld.hello("outofmemory.cn"));  
            System.exit(0);  
        }  
    }
    运行client,可以得到hello outofmemory.cn的输出。

  • 相关阅读:
    Ogre的骨骼动画
    ID卡读取方法(用于区分ID卡读取出来的数据和一般人手录入的数据)
    FastSpring学习笔记一
    数学 方程的解
    单调栈+桶+分治 奇袭
    神奇DP [HNOI2004] 打砖块
    DFS 找硬币
    树DP 树上染色
    android 适配器Adpter的使用总结 之 BaseExpandableListAdapter
    Java删除文件夹以及文件夹下的子目录与文件
  • 原文地址:https://www.cnblogs.com/gwd1154978352/p/6831924.html
Copyright © 2011-2022 走看看