通过CXF方式实现webservice服务

一.CXF的介绍

 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服务。

二.需要的jar包

       准备: 新建工程 导入需要的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官方网站(http://cxf.apache.org/download.html)下载 apache-cxf-2.1.2.zip, 然后从apache-cxf-2.1.2/lib 目录中获得

三.以annotation注解方式实现发布webService应用

 

1.编写服务接口

 package me.cxfwebseivice;

 

import java.util.List;

import javax.jws.WebParam;
import javax.jws.WebService;
         

@WebService
public interface HelloWorld {
String sayHi(@WebParam(name="text")String text);
String sayHiToUser(User user);
String[] SayHiToUserList(List<User> userList);
 }

2.接口实现类

package me.cxfwebseivice;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;
@WebService(endpointInterface="me.cxfwebseivice.HelloWorld",serviceName="HelloWorld")
public class HelloWorldImpl implements HelloWorld{
     Map<Integer, User> users = new LinkedHashMap<Integer, User>();

     
     public String sayHi(String text) {
                 return "Hello " + text;
    }



    public String sayHiToUser(User user) {
              users.put(users.size()+1, user);
              return "Hello "+ user.getName();
    }

    public String[] SayHiToUserList(List<User> userList) {
              String[] result = new String[userList.size()];
              int i=0;
              for(User u:userList){
                   result[i] = "Hello " + u.getName();
                   i++;
              }
      return result;
    }
}

3.附user实体类

package me.cxfwebseivice;

public class User {
  public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
private int id;
  private String name;
}

4. 发布服务app

package me.cxfwebseivice;

import javax.xml.ws.Endpoint;

public class webServiceApp {
public static void main(String[] args) {
    System.out.println("web service start");
    HelloWorldImpl implementor= new HelloWorldImpl();
    String address="http://localhost:8081/helloWorld";
    Endpoint.publish(address, implementor);
    System.out.println("web service started");
}
}

右键 run as 选择java application发布服务;然后在浏览器输入地址:http://localhost:8081/helloWorld?wsdl

说明服务发布成功

5.客户端访问服务

package me.cxfwebseivice;

import java.util.ArrayList;
import java.util.List;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloWorldClient {
public static void main(String[] args) {
JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
    svr.setServiceClass(HelloWorld.class);
    svr.setAddress("http://localhost:8081/helloWorld");
    HelloWorld hw = (HelloWorld) svr.create();
    User user = new User();
    user.setName("Tony");
    //user.setDescription("test");
    System.out.println(hw.sayHiToUser(user));
         
}
}

右键 run as 选择java application,控制台打印如图:

说明客户端调用服务成功

四.与spring集成实现webService

1.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>TestExcel</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
   <servlet>
 <servlet-name>CXFServlet</servlet-name>
  <servlet-class>
 org.apache.cxf.transport.servlet.CXFServlet
 </servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
               <servlet-mapping>
                      <servlet-name>CXFServlet</servlet-name>
                      <url-pattern>/webservice/*</url-pattern>
               </servlet-mapping>
</web-app>

2.配置spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="
             http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans.xsd
             http://cxf.apache.org/jaxws 
             http://cxf.apache.org/schemas/jaxws.xsd">

      <import resource="classpath:META-INF/cxf/cxf.xml"/>
      <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
      <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

      <jaxws:endpoint 
             id="helloWorld"
             implementor="me.cxfwebseivice.HelloWorldImpl"
             address="/helloWorld" />

     <bean id="client" 
             class="me.cxfwebseivice.HelloWorld" 
             factory-bean="clientFactory" 
             factory-method="create"/>

     <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
            <property name="serviceClass" value="me.cxfwebseivice.HelloWorld"/>
            <property name="address" value="http://localhost:7020/TestExcel/webservice/helloWorld"/>
     </bean>
     
</beans>

3.启动tomcat.发布webservice服务

访问http://localhost:7020/TestExcel/webservice/helloWorld?wsdl

ok,说明服务发布成功

4.修改客户端代码

package me.cxfwebseivice;

import java.util.ArrayList;
import java.util.List;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloWorldClient {
public static void main(String[] args) {
/*    JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
    svr.setServiceClass(HelloWorld.class);
    svr.setAddress("http://localhost:8080/helloWorld");
    HelloWorld hw = (HelloWorld) svr.create();
    User user = new User();
    user.setName("Tony");
    //user.setDescription("test");
    System.out.println(hw.sayHiToUser(user));*/
    ApplicationContext context = new ClassPathXmlApplicationContext("/spring.xml");
    HelloWorld client = (HelloWorld)context.getBean("client");
    User user1 = new User();
    user1.setName("Tony");
   // user1.setDescription("test");
    User user2 = new User();
    user2.setName("freeman");
    //user2.setDescription("test");
    List<User> userList= new ArrayList<User>();
    userList.add(user1);
    userList.add(user2);
    String[] res = client.SayHiToUserList(userList);
    System.out.println(res[0]);
    System.out.println(res[1]);          
}
}

右键run as application,控制台如下

ok,客户端调用服务成功

此篇实现了webService服务的发布以及在本工程下的客户端调用服务的示例

 

posted @ 2016-05-17 17:10  小柳爱吃肉  阅读(5854)  评论(0编辑  收藏  举报