S2S4H3整合cxf
準備:
我是已經有了一個S2S4H3的整合項目,由於需要,整合了cxf,估計看見這篇文檔的人都是知道cxf框架是幹什麼的吧,
爲了發佈WebService,要做一些前期準備,當然S2S4H3的項目肯定要有了,在這裏我就不解釋SSH的整合啦,關注我的下篇文檔吧。
① 準備cxf的jar包:
我用的是cxf 2.5.9,在官網可以下載到的,此整合教程是使用與3.0前的版本,因爲cxf 3.0之後的配置是不一樣的。
cxf-2.5.9.jar
cxf-2.5.9.jar
geronimo-servlet_2.5_spec-1.1.2.jar
neethi-3.0.2.jar
stax2-api-3.1.1.jar
woodstox-core-asl-4.1.4.jar
xmlschema-core-2.0.3.jar
② 在web.xml中新添如下配置:
還有一問題需要注意:因爲struts2 的默認過濾器默認是過濾捕捉所以請求的,所以需要自定義Filter爲cxf放行
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>30</load-on-startup>
</servlet>
<!-- 需要注意:cxfServlet與struts2的URL不能都寫/*,需要區分,在自定義Filter中對cxf放行,不然請求時會在struts2裏出錯-->
<servlet-mapping>
<servlet-name>sshserver </servlet-name>
<url-pattern>/sshserver/*</url-pattern>
</servlet-mapping>
<!-- 配置struts2的filter:自定義的Filter(爲了給cxf放行)-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.hxb.struts2.Filter.LetCxfGoFilter</filter-class>
</filter>
③ 在spring的配置文件ApplicationContext.xml中添加新的配置如下:
其中紅色字體爲cxf新添的配置。
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 導入cxf資源文件 -->
<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="orderProcess"implementor="org.hxb.WebService.ServerImpl"
address="/ServerPublish"/>
④ 自定義的Filter:
前面已經提過自定義Filter的目的是爲了給cxf放行:
package org.hxb.struts2.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.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;
/**
* <p>自定義Filter 爲cxf放行</p>
*
*/
public class LetCxfGoFilter extends StrutsPrepareAndExecuteFilter {
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChainarg2) throws IOException, ServletException {
HttpServletRequestrequest = (HttpServletRequest) arg0;
//如果路徑包含cxf則放行。
if(request.getRequestURI().contains("sshserver")){
arg2.doFilter(arg0, arg1);
}else{
super.doFilter(arg0,arg1, arg2);
}
}
}
⑤ Web Service的接口:
package org.hxb.WebService;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
publicinterface Server {
@WebMethod
public String getGasData();
}
⑥ Web Service的實現:
package org.hxb.WebService;
import javax.jws.WebService;
@WebService
publicclass ServerImpl implements Server {
@Override
public String getGasData() {
System.out.println("INFO:Server已經啓動!");
return"Service";
}
}
⑦ 啓動後:瀏覽器訪問:http://localhost:8080/sshtest/ /ServerPublish?wsdl