基于CXF搭建webService的實(shí)例講解
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"http://類所在地址或者#+對應(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 { // 默認(rèn)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)的。
以上這篇基于CXF搭建webService的實(shí)例講解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)選中刪除功能的實(shí)例代碼
這篇文章主要介紹了java實(shí)現(xiàn)選中刪除功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02基于Java 數(shù)組內(nèi)存分配的相關(guān)問題
本篇文章是對Java中數(shù)組內(nèi)存分配進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Java Vector和ArrayList的異同分析及實(shí)例講解
在本篇文章里小編給大家整理的是一篇關(guān)于Java Vector和ArrayList的異同分析及實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2021-01-01淺談Spring Boot 微服務(wù)項目的推薦部署方式
這篇文章主要介紹了淺談Spring Boot 微服務(wù)項目的推薦部署方式,具有一定參考價值,需要的朋友可以了解下。2017-09-09Java使用Jedis操作Redis服務(wù)器的實(shí)例代碼
本篇文章主要介紹了Java使用Jedis操作Redis服務(wù)器的實(shí)例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08springboot實(shí)現(xiàn)跨域的五種方式總結(jié)
在Spring Boot中實(shí)現(xiàn)跨域,可以采用全局跨域和局部跨域兩種方式,下面這篇文章主要給大家介紹了關(guān)于springboot實(shí)現(xiàn)跨域的五種方式,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01