导入CXF相关的jar包
在 web.xml 中加入 CXF Service 配置:
<servlet>
<servlet-name>cxfServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxfServlet</servlet-name>
<url-pattern>/cxfTest/*</url-pattern>
</servlet-mapping>
在配置struts2的拦截器时要注意可能会与CXF冲突:
<filter>
<filter-name>struts2</filter-name>
<filter-class>com.xxxxxxxx.Struts2Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
struts2会拦截cxf的请求,解决方法使用自定义的拦截器:
public class Struts2Filter extends StrutsPrepareAndExecuteFilter {
public void doFilter(ServletRequest req, ServletResponse response, FilterChain chain)throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
if(request.getRequestURI().contains("/cxfTest")){
chain.doFilter(request, response);
}else{
super.doFilter(request, response, chain);
}
}
}
application-cxf.cml:
<?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:cxf="http://cxf.apache.org/core"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/core
http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
<bean id="weatherService" class="com.********.WeatherService">
</bean>
<jaxws:server address="/weather">
<jaxws:serviceBean >
<ref bean="weatherService" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
发布webService之后:
使用
wsimport -s . http://localhost:8888/WeatherService/cxfTest/weather?wsdl
命令来生成方法。
配置application-cxf.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:cxf="http://cxf.apache.org/core"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/core
http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
">
<description>CXF</description>
<jaxws:client id="weatherClient" address="http://localhost:8888/WeatherService/cxfTest/weather?wsdl" serviceClass="com.*****.WeatherService"></jaxws:client>
</beans>
利用生成的接口进行调用即可。