微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 CXF 做 webservice 简单例子

转:http://www.cnblogs.com/frankliiu-java/articles/1641949.html

Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的服务器上,例如 Jboss、IBM® WebSphere® 或 BEA WebLogic。

 

       该框架提供了以下功能

  • Web 服务标准支持CXF 支持以下 Web 服务标准:
    • Java API for XML Web Services (JAX-WS)
    • SOAP
    • Web 服务描述语言(Web Services Description Language ,WSDL)
    • 消息传输优化机制(Message Transmission Optimization Mechanism,MTOM)
    • WS-Basic Profile
    • WS-Addressing
    • WS-Policy
    • WS-ReliableMessaging
    • WS-Security
  • 前端建模:CXF 提供了前端建模的概念,允许您使用不同的前端 API 来创建 Web 服务。API 允许您使用简单的工厂 Bean 并通过 JAX-WAS 实现来创建 Web 服务。它还允许您创建动态 Web 服务客户端。
  • 工具支持CXF 提供了用于在 Java Bean、Web 服务和 WSDL 之间进行转换的不同工具。它提供了对 Maven 和 Ant 集成的支持,并无缝地支持 Spring 集成。
  • RESTful 服务支持CXF 支持代表性状态传输(Representational State Transfer,RESTful )服务的概念,并支持 Java 平台的 JAX-RS 实现。(本系列的第 2 部分将提供有关 RESTful 服务的更多信息。)
  • 对不同传输和绑定的支持CXF 支持不同种类的传输,从 XML 到逗号分隔值 (CSV)。除了支持 SOAP 和 HTTP 协议绑定之外,它还支持 Java Architecture for XML Binding (JAXB) 和 AEGIS 数据绑定。
  • 对非 XML 绑定的支持CXF 支持非 XML 绑定,例如 JavaScript Object Notation (JSON) 和 Common Object Request broker Architecture (CORBA)。它还支持 Java 业务集成(Java Business Integration,JBI)体系架构和服务组件体系架构(Service Component Architecture,SCA)。
  • code first 或者 xml first  : 支持使用code first 或者 xml first 的方式来创建web服务。

       一  借助 annotation 创建独立启动的web 服务。

       准备: 新建工程 导入需要的jar 包:  

                  依赖的包:

                            commons-logging-1.1.jar
                            geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)
                            geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
                            geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar)
                            geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar)
                            geronimo-ws-Metadata_2.0_spec-1.1.1.jar (JSR 181)
                            jaxb-api-2.1.jar
                            jaxb-impl-2.1.6.jar
                            jaxws-api-2.1.jar
                            jetty-6.1.5.jar
                            jetty-util-6.1.5.jar
                            neethi-2.0.jar
                            saaj-api-1.3.jar
                            saaj-impl-1.3.jar
                            stax-api-1.0.1.jar
                            wsdl4j-1.6.1.jar
                            wstx-asl-3.2.1.jar
                            XmlSchema-1.2.jar
                            xml-resolver-1.2.jar     

                  spring jar 包, 用来支持xml配置:

                            aopalliance-1.0.jar
                            spring-core-2.0.4.jar
                            spring-beans-2.0.4.jar
                            spring-context-2.0.4.jar
                            spring-web-2.0.4.jar

                   CXF jar包

                            cxf-2.1.jar

   

         以上jar 包 可从apache官方网站下载 apache-cxf-2.1.2.zip, 然后从apache-cxf-2.1.2/lib 目录中获得

      1  首先服务点接口。

[java]  view plain copy
  1. package com.demo;  
  2.   
  3.           import java.util.List;  
  4. import javax.jws.WebParam;  
  5.           import javax.jws.WebService;  
  6.            
  7.           @WebService  
  8. public interface HelloWorld {  
  9.                String sayHi(@WebParam(name="text")String text);  
  10.                String sayHiToUser(User user);  
  11.                String[] SayHiToUserList(List<User> userList);  
  12.            }  

·     2  编写服务实现

copy
            import java.util.LinkedHashMap;  
  1.         import java.util.List;  
  2. import java.util.Map;  
  3. import javax.jws.WebService;  
  4.   
  5.         @WebService(endpointInterface="com.demo.HelloWorld",serviceName="HelloWorld")  
  6. class HelloWorldImpl implements HelloWorld {  
  7.                    Map<Integer, User> users = new LinkedHashMap<Integer, User>();  
  8.                    public String sayHi(String text) {  
  9.                                return "Hello " + text;  
  10.                   }  
  11.                   public String sayHiToUser(User user) {  
  12.                             users.put(users.size()+1, user);  
  13.                             return "Hello "+ user.getName();  
  14.                   }  
  15.                   public String[] SayHiToUserList(List<User> userList) {  
  16.                             String[] result = new String[userList.size()];  
  17.                             int i=0;  
  18. for(User u:userList){  
  19.                                  result[i] = "Hello " + u.getName();  
  20.                                  i++;  
  21.                             }  
  22.                     return result;  
  23.       }  

 3  编写 webServiceApp.java类来暴露 web服务。

@H_363_404@copy
          import javax.xml.ws.Endpoint;  
  1.    
  2. class webServiceApp {  
  3.                  static void main(String[] args) {  
  4.                            System.out.println("web service start");  
  5.                            HelloWorldImpl implementor= new HelloWorldImpl();  
  6.                            String address="http://localhost:8080/helloWorld";  
  7.                            Endpoint.publish(address, implementor);  
  8.                            System.out.println("web service started");  
  9. 4  run webServiceApp.java 类来启动服务。 访问 http://localhost:8080/helloWorld?wsdl  查看是否显示

         wsdl。

     5  编写客户端访问服务。

    copy
         import java.util.ArrayList;  
  1.      import org.apache.cxf.jaxws.JaxWsProxyfactorybean;  
  2. import org.springframework.context.support.ClasspathXmlApplicationContext;  
  3. class HelloWorldClient {  
  4.                                        JaxWsProxyfactorybean svr = new JaxWsProxyfactorybean();  
  5.                         svr.setServiceClass(HelloWorld.class);  
  6.                         svr.setAddress("http://localhost:8080/helloWorld");  
  7.                         HelloWorld hw = (HelloWorld) svr.create();  
  8.                         User user = new User();  
  9.                         user.setName("Tony");  
  10.                         user.setDescription("test");  
  11.                         System.out.println(hw.sayHiToUser(user));  
  12.               }  
  13.      }  

 6  测试: run webServiceApp.java 类来启动服务,然后 run HelloWorldClient.java 来访问服务。

 二 集成到spring 中。

1 在 web.xml 中加入 :

[html]  copy
    <?xml version="1.0" encoding="UTF-8"?>  
  1.       <web-app>  
  2.                welcome-file-list>  
  3.                         welcome-file>index.jsp</                context-param                          param-name>contextConfigLocation                          param-value>WEB-INF/classes/applicationContext.xml                               listener                      listener-class                              org.springframework.web.context.ContextLoaderListener  
  4. servlet                     servlet-name>CXFServlet                     display-nameservlet-class                            org.apache.cxf.transport.servlet.CXFServlet  
  5. load-on-startup>1               servlet-mapping                      url-pattern>/webservice/*                        >  

2  在 applicationContext.xml 中加入:
copy
        beans xmlns="http://www.springframework.org/schema/beans"  
  1.                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.                  xmlns:jaxws="http://cxf.apache.org/jaxws"  
  3.                  xsi:schemaLocation="  
  4.                        http://www.springframework.org/schema/beans  
  5.                        http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.                        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"    
  7. import resource="classpath:meta-inf/cxf/cxf.xml"/>  
  8.                 import resource="classpath:meta-inf/cxf/cxf-extension-soap.xml"/>  
  9. import resource="classpath:meta-inf/cxf/cxf-servlet.xml"                  jaxws:endpoint   
  10.                               id="helloWorld"  
  11.                               implementor="com.demo.HelloWorldImpl"  
  12.                               address="/helloWorld"       
  13. bean id="client" class="com.demo.HelloWorld"   
  14.                            factory-bean="clientFactory" factory-method="create"       
  15. bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyfactorybean"                             property name="serviceClass" value="com.demo.HelloWorld"                             property name="address"  
  16.                                               value="http://localhost:8080/s/webservice/helloWorld"                   bean      beans 注意: 这里需要加入  xmlns:jaxws="http://cxf.apache.org/jaxws"   

                  http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd

     3  修改客户端。
    copy
                              HelloWorld client = (HelloWorld)context.getBean("client");  
    1.                         User user1 = new User();  
    2.                         user1.setName("Tony");  
    3.                         user1.setDescription("test");  
    4.                         User user2 =                          user2.setName("freeman");  
    5.                         user2.setDescription("test");  
    6.                         List<User> userList= new ArrayList<User>();  
    7.                         userList.add(user1);  
    8.                         userList.add(user2);  
    9.                         String[] res = client.SayHiToUserList(userList);  
    10.                         System.out.println(res[0]);  
    11.                         System.out.println(res[1]);            
    12.  4  发布工程 启动web服务器(我用 tomcat 6)。
      5 访问 http://localhost:8080/s/webservice/helloWorld?wsdl  查看是否显示 wsdl 。

       6  run run HelloWorldClient.java 来访问服务。

      版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

      相关推荐