天天看點

使用CXF 來釋出一個 service

參考官網文檔:​​http://cxf.apache.org/docs/writing-a-service-with-spring.html​​

從官網上下載下傳 cxf 的包,包裡會有 samples 檔案夾,該檔案夾中存放的就是cxf 的一些小例子

這裡就是針對 java_first_spring_support 例子的改寫 與 說明,該例子是采用 spring +maven +cxf 技術

建立項目

  • 使用Eclipse 建立一個 Maven Project,工程名為 TestCXFDome1 ,修改pom.xml 引入需要使用的jar 包
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>panie</groupId>
    <artifactId>TestCXFDome1</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>TestCXFDome1 Maven Webapp</name>
    <url>http://maven.apache.org</url>      

編寫服務端

  • 首先寫一個接口。@WebService注解表示是要釋出的web服務
package demo.spring.service;
 
import javax.jws.WebService;
 
@WebService
public interface HelloWorld {
    String sayHi(String text);
}      
  • 對該接口寫一個實作類。@WebService注解表示是要釋出的web服務,endpointInterface參數的值是該服務類對應的接口。
package demo.spring.service;
 
import javax.jws.WebService;
 
@WebService(endpointInterface = "demo.spring.service.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
 
    public String sayHi(String text) {
        System.out.println("sayHi called");
        return "Hello " + text;
    }
}      
  • 聲明 server bean

  CXF 支援 Spring 2.0 Schema 标簽配置方式,并且提供快捷暴露 Web Services 的标簽。對于 JAX_WS 端,我們可以使用<jaxws:endpoint> 來設定伺服器端的 endpoint (請參考英文原文)

  1)我們需要引入 Spring 與 CXF 的命名空間(namespace),這樣,我們可以使用 Spring 與 CXF 的标簽配置了。  2)我們需要引入我們所需要的 CXF 的 Bean 定義檔案

  3)定義我們具體實作的 Bean

  在 WEB-INF 目錄下建立一個 cxf-servlet.xml 檔案來聲明 一個endpoint bean

<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      

  如果 想引入 spring 來管理bean,可以這樣寫

<bean id="hello" class="demo.spring.service.HelloWorldImpl" />
 
<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />      

  id - 指在 spring context 中的bean id

  implementor - 指定 實作類。當使用bean 來表示實作類時,使用 #bean-name

  address - 指 服務将會被釋出的位置,這裡隻能使用相對路徑。因為CXF 不知道 war 名字,也不知道 容器端口,CXF 運作時将會更新 endpoint 位址

  • 配置Servlet

  如果采用 預設路徑的 cxf-servlet.xml ,在web.xml 中配置

class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>      

  如果 cxf-servlet.xml  更換的位置 或者 名字,如改為 beans.xml ,則需要在 web.xml 中配置

<context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:/beans.xml</param-value>
   </context-param>
 
   <listener>
      <listener-class>
         org.springframework.web.context.ContextLoaderListener
      </listener-class>
   </listener>      

部署到web伺服器上,釋出webservice工程,輸入http://localhost:8080/TestCXFDome1/HelloWorld?wsdl 

編寫用戶端(一)

   在用戶端可以使用 <jaxws:client> 來聲明。你隻需要提供 bean name,服務接口,服務請求URL,它就可以在遠端SOAP伺服器上建立一個指定名稱的bean來實作服務接口。

  • 配置一個 client-bean.xml
<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/schema/jaxws.xsd">
    <bean id="client" class="demo.spring.service.HelloWorld" factory-bean="clientFactory" factory-method="create"/>
    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="demo.spring.service.HelloWorld"/>
        <property name="address" value="http://localhost:8080/TestCXFDome1/HelloWorld"/>
    </bean>
</beans>      
  • 編寫一個 測試類 Client
package demo.spring.client;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import demo.spring.service.HelloWorld;


public final class Client {

    private Client() {
    }

    public static void main(String args[]) throws Exception {
        // START SNIPPET: client
        ClassPathXmlApplicationContext context 
            = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});

        HelloWorld client = (HelloWorld)context.getBean("client");

        String response = client.sayHi("Joe");
        System.out.println("Response: " + response);
        System.exit(0);
        // END SNIPPET: client
    }
}      

運作 測試類,即可看到 調用 服務的結果

編寫用戶端(二)

package demo.spring.client;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import demo.spring.service.HelloWorld;

public class Client2
{
    public static void main(String[] args)
    {
      //建立WebService用戶端代理工廠
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        //注冊WebService接口
        factory.setServiceClass(HelloWorld.class);
        //設定WebService位址
        factory.setAddress("http://localhost:8180/TestCXFDome1/HelloWorld");
        HelloWorld hello = (HelloWorld)factory.create();
        //調用webservice接口方法
        String response = hello.sayHi("panie");//傳回sucess
        System.out.println("Response: " + response);
    }
}