java之Spring集成CXF簡單調用

簡介

Apache CXF = Celtix + XFire,開始叫 Apache CeltiXfire,後來改名爲 Apache CXF 了,如下簡稱爲 CXF。CXF 繼承了 Celtix 和 XFire 兩大開源項目的精華,提供了對 JAX-WS 全面的支持,而且提供了多種 Binding 、DataBinding、Transport 以及各類 Format 的支持,而且能夠根據實際項目的須要,採用代碼優先(Code First)或者 WSDL 優先(WSDL First)來輕鬆地實現 Web Services 的發佈和使用。Apache CXF 已是一個正式的 Apache 頂級項目。java

簡單使用

編寫服務端

一、新建 maven web 工程做爲服務端,引入以下依賴:web

<?xml version="1.0" encoding="UTF-8"?>

<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.zze</groupId>
  <artifactId>cxf_server</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <dependencies>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <!--使用 tomcat7:run-->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <port>8080</port>
          <path>/</path>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
pom.xml

二、新建服務類接口及其實現類:spring

package com.zze.webservice;

import javax.jws.WebService;
@WebService
public interface ITestService {
    String hello();
}
com.zze.webservice.ITestService
package com.zze.webservice.impl;

import com.zze.webservice.ITestService;

public class TestService implements ITestService {
    public String hello() {
        return "hello CXF";
    }
}
com.zze.webservice.impl.TestService

三、在 Spring 配置文件中發佈服務:apache

<?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">
    <!--調用的類-->
    <bean id="testService" class="com.zze.webservice.impl.TestService"></bean>
    <!--發佈服務-->
    <jaxws:server address="/testService">
        <jaxws:serviceBean>
            <ref bean="testService"/>
        </jaxws:serviceBean>
    </jaxws:server>
</beans>
applicationContext_cxf.xml

四、配置 Spring 監聽器及 CXF 過濾器:tomcat

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext_cxf.xml</param-value>
    </context-param>
    <filter>
        <filter-name>cxf</filter-name>
        <filter-class>org.apache.cxf.transport.servlet.CXFServlet</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>cxf</filter-name>
        <url-pattern>/ws/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>
WEB-INF/web.xml

五、啓動程序(服務隨之發佈),測試服務是否發佈成功,發佈成功則以下:app

編寫客戶端

一、新建 maven java 工程做爲客戶端,引入以下依賴:frontend

<?xml version="1.0" encoding="UTF-8"?>
<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.zze</groupId>
    <artifactId>cxf_client</artifactId>
    <version>1.0-SNAPSHOT</version>

   <dependencies>
       <dependency>
           <groupId>org.apache.cxf</groupId>
           <artifactId>cxf-rt-frontend-jaxws</artifactId>
           <version>3.0.1</version>
       </dependency>
       <dependency>
           <groupId>org.apache.cxf</groupId>
           <artifactId>cxf-rt-transports-http</artifactId>
           <version>3.0.1</version>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>4.2.4.RELEASE</version>
       </dependency>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.12</version>
           <scope>test</scope>
       </dependency>
   </dependencies>
</project>
pom.xml

二、cmd 進入 classpath 目錄下,執行以下命令生成客戶端代碼:maven

wsimport -s . http://localhost:8080/ws/testService?wsdl

三、在 Spring 配置文件中配置調用服務的客戶端實例:ide

<?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">
    <jaxws:client id="testClient" address="http://localhost:8080/ws/testService?wsdl"
                  serviceClass="com.zze.webservice.impl.ITestService"/>
</beans>
applicationContext_cxf.xml

四、編寫代碼調用服務端,以下輸出則調用成功:測試

package com.zze.webservice.test;

import com.zze.webservice.impl.ITestService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CXFTest{
    @Test
    public void test1() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext_cxf.xml");
        ITestService testClient = (ITestService)applicationContext.getBean("testClient");
        System.out.println(testClient.hello());
    }
}

com.zze.webservice.test.CXFTest
相關文章
相關標籤/搜索