2017年1月26日 星期四

CXF 與 Spring 整合 ( WebService 四)

官網的maven 設定連結
官網的spring 設定連結

※pom.xml

<properties>
    <cxf.version>3.1.9</cxf.version>
    <spring.version>3.2.15.RELEASE</spring.version>
</properties>
    
<dependencies>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http-jetty</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
</dependencies>

※Spring基本要兩個包,core和context,但因為要run server,所以還要加web包



※伺服器端

要整合Spring,伺服器端要Web專案才可以


※web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
    
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
    
<servlet>
    <servlet-name>cxf</servlet-name>
    <servlet-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>

※<servlet-class>的路徑在Servlet Transport能找到

※<servlet-class>的CXFServlet在cxf-rt-transports-http-x.x.x.jar裡面



※applicationContext.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-servlet.xml" />
    
    <!-- <bean id="hs" class="hello.test.HelloWS" /> -->
    <!-- <jaxws:endpoint implementor="#hs" address="/aaa/bbb/ccc" /> -->
    
    <jaxws:endpoint implementor="hello.test.HelloWS" address="/aaa/bbb/ccc" />
</beans>

※註解的部分是第二種方法

※address是要接在IP後的

※SEI和之前一樣

※cxf.xml在cxf-core-x.x.xjar裡面的「META-INF\cxf」

※cxf-servlet.xml在cxf-rt-transports-http-x.x.x.jar裡面的「META-INF\cxf」

※如果有cxf-extension-soap.xml,可在cxf-rt-bindings-soap-2.2.3.jar 裡面的「META-INF\cxf」找到

※執行後會自動run出如下的畫面

※所以整合後,原本的網址會變成:IP加上專案名稱

※wsdl如下:

※aaa/bbb/ccc 是在 applicationContext.xml 裡設定的

※完成後就可用類似 SoapUI的軟體測試



※用戶端

用一般的專案即可,不用用到web專案,當然要用也行
用wsimport 或 wsdl2java 產生用戶端程式碼後,裡面只有一隻interface


※applicationContext.xml

    
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:jaxws="http://cxf.apache.org/jaxws"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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="hws" 
        serviceClass="hello.test.IHelloWS"
        address="http://localhost:8080/SCXF/aaa/bbb/ccc" />
        
    <!--
        <bean id="hws" factory-bean="clientFactory" factory-method="create"/>
        <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
            <property name="serviceClass" value="hello.test.IHelloWS"/>
            <property name="address" value="http://localhost:8080/SCXF/aaa/bbb/ccc"/>
        </bean>
    -->
</beans>

※註解的部分是另外一種方法,看來CXF已幫我們寫好了,所以不一定要用jaxws的命名空間


※測試類

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IHelloWS client = (IHelloWS) context.getBean("hws");
System.out.println(client.add(50, 20));
context.close();



※攔截器


伺服器端和用戶端的java檔和上一篇一樣


※用戶端的applicationContext.xml

<jaxws:client id="hws" 
    serviceClass="hello.test.IHelloWS"
    address="http://localhost:8080/SCXF/aaa/bbb/ccc">
    
    <jaxws:outInterceptors>
        <bean class="AddLoginInfo">
            <constructor-arg name="account" value="bruce" />
            <constructor-arg name="password" value="12345" />
        </bean>
    </jaxws:outInterceptors>
</jaxws:client>




※伺服器端的applicationContext.xml

<jaxws:endpoint implementor="hello.test.HelloWS" address="/aaa/bbb/ccc">
    <jaxws:inInterceptors>
        <bean class="hello.test.CheckLoginInfo" />
    </jaxws:inInterceptors>
</jaxws:endpoint>




沒有留言:

張貼留言