快速上手使用CXF

 

快速上手使用CXF

一.  简介

Apache CXF 是一个Service框架,他简化了Service的创建, CXF实现了JAX-WS2.0规范,并通过了JAX-WS2.0 TCK CXFSpring无缝集成;CXF支持多种传输协议(HTTP JMS Corba等), 支持多种Binding数据格式(SOAPXMLJSON等), 支持多种DataBinding数据类型(JAXB Aegis) CXF基于Interceptor的架构,使得整个框架非常易于扩展。

二.  如何发布并调用简单的web service实例

2.1.下载:apache-cxf- 2.1.1   http://cxf.apache.org/download.html

2.2 新建java project ,并加入apache-cxf- 2.0.7 /lib所有包,编写要发布的web service 接口和实现

import javax.jws.WebService;

@WebService 

public interface HelloWorld {  

     public String sayHello(String text);  

}

import javax.jws.WebService;  

@WebService(endpointInterface="test.HelloWorld")  

public class HelloWorldImpl implements HelloWorld {  

      public String sayHello(String text) {  

                  return "Hello" + text ;  

    }  

  } 

@WebService 注解表示是要发布的web 服务

name:用于Interface,属映射到wsdl:portType elementname属性。
targetNamespace
:用于Interfaceimplement,如果不指定,缺省会使用包名倒序做为wsdl名空间。

serviceName:用于implement,表示wsdl服务名。

portName:用于implement,表示wsdl:port name属性。

endpointInterface:用于implement,指定Interface全名,包括包名。

2.3.发布web service

public class Server {

    protected Server() throws Exception {

         System.out.println("Starting Server");

         HelloWorldImpl implementor = new HelloWorldImpl();

         String address = "http://localhost:9000/helloWorld";

         Endpoint.publish(address, implementor);

    }

    public static void main(String args[]) throws Exception {

        new Server();

        System.out.println("Server ready...");

        Thread.sleep(5 * 60 * 1000);

        System.out.println("Server exiting");

        System.exit(0);

    }

}

运行后,在浏览器中输入http://localhost:9000/helloWorld?wsdl将显示这个web servicewsdl.说明web service发布成功。

2.4.下面就开始创建一个客户端程序,访问这个web service, 同样新建java project ,并加入apache-cxf- 2.0.7 /lib所有包,由于CXF已经提供wsdl转化成java 的命令工具,所以创建一个build.xml,用来生成客户端程序。Bulid.xml内容如下:

<?xml version="1.0"?>

<project name="cxf wsdl2java" basedir=".">  

   <property name="cxf.home" location ="${basedir}/WebRoot/WEB-INF/"/>

   <path id="cxf.classpath">

      <fileset dir="${cxf.home}/lib">

         <include name="*.jar"/>

      </fileset>

   </path>     

   <target name="cxfWSDLToJava">

      <java classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="true">

         <arg value="-client"/>

         <arg value="-d"/>

         <arg value="src"/>

         <arg value="http://localhost:9000/helloWorld?wsdl"/>

         <classpath>

            <path refid="cxf.classpath"/>

         </classpath>

      </java>

   </target>

</project>

或者:配置环境变量%CXF_HOME%=E:/WebService/CXF/apache-cxf- 2.1.1 /apache-cxf-2.1.1(以我的目录为例),并在PATH后加上;%CXF_HOME%/bin

cmd命令行中输入wsdl2java如果显示其用法表示配置好了。

输入:wsdl2java -d src - client http://localhost:9000/helloWorld?wsdl

其作用上面的build.xml作用一样。

附加:wsdl2java用法:

wsdl2java -p com -d src -all  aa.wsdl

-p  指定其wsdl的命名空间,也就是要生成代码的包名:

-d  指定要产生代码所在目录

-client 生成客户端测试web service的代码

-server 生成服务器启动web  service的代码

-impl 生成web service的实现代码

-ant  生成build.xml文件

-all 生成所有开始端点代码:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.

详细用法见:http://cwiki.apache.org/CXF20DOC/wsdl-to-java.html

2.5.调用web service

public class MyClient {

      public static void main(String[] argv) {

        HelloWorld  hello = new HelloWorldImplService().getHelloWorldImplPort();

        System.out.println(hello.sayHello("Tom") ); 

      }

    }

注意:运行时,要一定先要发布web sevice.

三.  参考资料

1.CXF 主页: http://cxf.apache.org/

2. CXF中文讨论组: http://groups.google.com/group/cxf-zh 

3. Web service: http://www.w3school.com.cn/webservices/index.asp
4. WSDL:
http://www.w3school.com.cn/wsdl/index.asp
5. SOAP:
http://www.w3school.com.cn/soap/index.asp

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值