Web Service學習-CXF與Spring整合為JavaEE應用發布WebService(三)

jopen 8年前發布 | 14K 次閱讀 WEB服務/RPC/SOA

CXFSpring整合,分兩個方面給大家介紹:

 

1,在傳統ssh項目基礎上添加Web Service

賦值CXFjar

web.xml配置文件中導入CXF的核心控制器:CXFServlet

Spring配置文件中導入CXF提供Schemaxml配置文件

Spring配置文件中使用jaxws:endpoint元素來暴露Web Service

如果要添加攔截器,在jaxws:endpoint元素里添加

inInterceptors,outInterceptors子元素

2,遠程調用Web Service服務(讓Action依賴遠程Web Service的接口)

復制CXFjar

Spring配置文件中導入CXF提供Schemaxml配置文件

Spring配置文件中使用jaxws:client元素來配置遠程Web Service代理

如果要添加攔截器。在jaxws:client元素里添加

inInterceptors,outInterceptors子元素

 

第一個方面:提供Web Service的服務


服務端:

 

項目結構:




在相應的SSH項目中添加CXF的相關jar包:




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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  

    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>/WEB-INF/applicationContext-common.xml</param-value>  
    </context-param>  
      <!--所有來自/*的請求,都交由 CXFServlet來處理-->
    <servlet>  
        <servlet-name>HelloWorldService</servlet-name>  
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>HelloWorldService</servlet-name>  
        <url-pattern>/*</url-pattern>  
    </servlet-mapping>  

</web-app>

applicationContext-common.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" />  

    <bean id="helloWorld" class="com.tgb.service.impl.HelloWorldImpl">
        <property name="userService" ref="userService"></property>
    </bean>  

  <!-- 用戶的Service -->    
    <bean id="userService" class="com.tgb.service.impl.UserServiceImpl">       
    </bean>
    <!-- implementor指定webservice的服務提供者 -->
    <!-- address為wsdl的訪問地址 -->
    <jaxws:endpoint id="hello" implementor="#helloWorld" address="/hjy" >
        <!-- 添加了2個In攔截器,如果不添加攔截器可直接注釋掉如下代碼 -->
        <jaxws:inInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
            <bean class="com.tgb.auth.AuthInterceptor"/>
        </jaxws:inInterceptors>
        <!-- 如果要配置Out攔截器,使用outInterceptors -->
    </jaxws:endpoint>
</beans>

以上配置已經完成,對于接口和相應的實現參考之前的博客即可,實現中對于new的內容使用spring管理起來

 

HelloWorldImpl:

package com.tgb.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import com.tgb.domain.Cat;
import com.tgb.domain.User;
import com.tgb.service.HelloWorld;
import com.tgb.service.UserService;

@WebService(endpointInterface="com.tgb.service.HelloWorld",serviceName="HelloWorldImpl")
public class HelloWorldImpl implements HelloWorld{

    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String sayHi(String name) {

        return name+"您好!現在時間為:"+new Date();
    }

    @Override
    public List<Cat> getCatsByUser(User user) {
        //在實際項目中,web service組件自己并不會去實現業務功能
        //它只是調用業務邏輯組件的方法來暴露web service
//      UserService us=new UserServiceImpl();

        return userService.getCatsByUser(user);
    }

    @Override
    public Map<String, Cat> getAllCats() {
//      UserService us=new UserServiceImpl();
        return userService.getAllCats();
    }


}

啟動tomcat服務器:

 

訪問如下地址:http://192.168.24.215:8080/CXF_Spring_Server





新建客戶端項目CXF_Spring_Client,生成客戶端代碼:




注意:

有些版本拷貝后,類中的super()會出錯,要加上-frontendjaxws21,參看如上截圖

 

客戶端調用:


package com.tgb.client;

import java.util.List;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;

import com.tgb.auth.AddHeaderInterceptor;
import com.tgb.service.Cat;
import com.tgb.service.HelloWorld;
import com.tgb.service.User;
import com.tgb.service.impl.HelloWorldImpl;

public class client {

    public static void main(String[] args){
        HelloWorldImpl factory=new HelloWorldImpl();
        //此處返回的只是遠程Web Service的代理
        HelloWorld hw=factory.getHelloWorldImplPort();

        /**
         * 添加的攔截器
         */
        Client client=ClientProxy.getClient(hw);
        //參數為輸入的用戶名,密碼
        client.getOutInterceptors().add(new AddHeaderInterceptor("hejingyuan","hjy"));


        System.out.println(hw.sayHi("hejingyuan"));     
        System.out.println("--------------------------");

        User user=new User();
        user.setId(20);
        user.setName("孫悟空");
        user.setPass("111");
        user.setAddress("花果山");

        List<Cat> cats=hw.getCatsByUser(user);
        for(Cat cat:cats){
            System.out.println(cat.getName());
        }

        System.out.println("--------------------------");

        System.out.println(hw.getAllCats().getEntry().get(0).getKey());
        System.out.println(hw.getAllCats().getEntry().get(0).getValue().getName());

    }
}



由于我們在服務端添加了攔截器,故客戶端必須要添加相應的攔截器給服務端提供參數,否則客戶端調用失敗




另一個方面:調用遠程的Web Service服務

 

新建客戶端項目CXF_Spring_Web_Client,生成客戶端代碼




web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <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>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

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-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <bean id="listCatsAction" class="com.tgb.action.ListCatsAction">
        <property name="helloWorld" ref="helloWorld"></property>
    </bean>

    <!-- 配置遠程webservice代理 -->
    <!-- id="helloWorld"對應 action setHelloWorld() -->
    <jaxws:client id="helloWorld"
        serviceClass="com.tgb.service.HelloWorld"
        address="http://localhost:8080/CXF_Spring_Server/hjy">
        <jaxws:outInterceptors>
        <!-- 配置輸出攔截器  --> 
            <bean class="com.tgb.auth.AddHeaderInterceptor" >
                <constructor-arg value="hejingyuan"/>
                <constructor-arg value="hjy"/>
            </bean>
        </jaxws:outInterceptors>
    </jaxws:client>

</beans>

Struts.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

    <package name="default" namespace="/" extends="struts-default">
        <action name="listCats" class="com.tgb.action.ListCatsAction" >
            <result name="SUCCESS">/WEB-INF/page/listcats.jsp</result>
        </action>
    </package>
</struts>

調用代碼:

package com.tgb.action;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;

import com.opensymphony.xwork2.ActionSupport;
import com.tgb.auth.AddHeaderInterceptor;
import com.tgb.service.HelloWorld;


public class ListCatsAction extends ActionSupport{


    private HelloWorld helloWorld;

    public HelloWorld getHelloWorld() {
        return helloWorld;
    }

    public void setHelloWorld(HelloWorld helloWorld) {
        this.helloWorld = helloWorld;
    }

    @Override
    public String execute() throws Exception {
        /**
         * 添加的攔截器
         */
        Client client=ClientProxy.getClient(helloWorld);
        //參數為輸入的用戶名,密碼
        client.getOutInterceptors().add(new AddHeaderInterceptor("hejingyuan","hjy"));
        helloWorld.sayHi("hejingyuan");             
        return "SUCCESS";
    }

}

最終的項目結構:




總結:


     以上與Spring的整合,概括的說,在應用Spring的框架過程中,一種是我們如何對外提供服務,另一種是我們如何獲取已經提供好的服務。在整個過程中,只有幾個關鍵點,很簡單,但是要注意jar包的版本問題。


源碼下載




來自: http://blog.csdn.net/hejingyuan6/article/details/47152237

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!