溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何基于CXF搭建webService

發(fā)布時間:2021-07-13 14:31:37 來源:億速云 閱讀:97 作者:小新 欄目:編程語言

這篇文章將為大家詳細講解有關(guān)如何基于CXF搭建webService,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

1.導(dǎo)入相關(guān)jar包,具體哪些包我記不太清了

2.在applicationContext中加入相關(guān)配置信息,如下所示:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jaxws="http://cxf.apache.org/jaxws" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
              http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
              http://www.springframework.org/schema/context 
              http://www.springframework.org/schema/context/spring-context-3.0.xsd 
              http://cxf.apache.org/jaxws 
              http://cxf.apache.org/schemas/jaxws.xsd"> 
 
    <context:component-scan base-package="com.cxf.bo"/> 
   
    <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="OrderWS" 
      implementor="com.cxf.spring.ws.OrderWSImpl"//類所在地址或者#+對應(yīng)bean的id
      address="/OrderWS" > //隨意命名
      <jaxws:features> 
       <bean class="org.apache.cxf.feature.LoggingFeature" /> 
      </jaxws:features> 
    </jaxws:endpoint> 
</beans>

3.在web.xml文件中加入:

<!-- cxf配置 -->
   <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>/CXFServlet/*</url-pattern> 
  </servlet-mapping>

4.在service層加入:

@WebService
public interface OrderWS {
  @WebMethod
  public Order getOrderById(int id);
}

5.若存在struts2,會發(fā)生沖突,則重寫過濾器

5.1 寫一個類ExtendStrutsFilter:

package com.nbu.retailer.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;

public class ExtendStrutsFilter extends StrutsPrepareAndExecuteFilter{
  private static Logger log = LoggerFactory.getLogger(ExtendStrutsFilter.class); 
  @Override 
  public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) 
      throws IOException, ServletException { 
      try { 
        HttpServletRequest request = (HttpServletRequest) req; 
        // 判斷是否是向WebService發(fā)出的請求 
        if (request.getRequestURI().contains("/CXFServlet")) { 
           // 如果是來自向CXFService發(fā)出的請求 
           chain.doFilter(req, res); 
         } 
         else { 
           // 默認action請求 
            super.doFilter(req, res, chain); 
         } 
       } 
      catch (Exception e) { 
         log.error(e.getMessage()); 
         e.printStackTrace(); 
      } 
  } 

}

5.2 在web.xml中改變過濾器的地址:

<!-- struts2的過濾器-->
   <filter>
    <filter-name>struts2</filter-name>
    <!--
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    -->
    <!-- 自定義struts2過慮器類 用于解決struts2把cxf的請求當(dāng)action處理的問題-->
    <filter-class>com.nbu.retailer.filter.ExtendStrutsFilter</filter-class>
  </filter>

5.3 注意,CXF的url和struts2的url不能相同。之前就這個問題困擾了我好久才發(fā)現(xiàn)的。

關(guān)于“如何基于CXF搭建webService”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI