基於CXF搭建webService,CXF搭建webService

來源:互聯網
上載者:User

基於CXF搭建webService,CXF搭建webService

1.匯入相關jar包,具體哪些包我記不太清了

2.在applicationContext中加入相關配置資訊,如下所示:

<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"//類所在地址或者#+對應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層加入:

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

 

5.若存在struts2,會發生衝突,則重寫過濾器

  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發出的請求                  if (request.getRequestURI().contains("/CXFServlet")) {                       // 如果是來自向CXFService發出的請求                       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的請求當action處理的問題-->        <filter-class>com.nbu.retailer.filter.ExtendStrutsFilter</filter-class>    </filter>


 

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.