您的位置:首页 > 其它

现有项目整合CXF实现webservice对接

2016-10-09 13:21 295 查看
我们现有的项目基础使用的spring和struts框架,为了实现webservice对接其他项目,最后决定使用apache的开源项目CXF,在百度上有许多的spring整合CXF的教程,但是多多少少都有一些疏漏。现在完成了整合后记录下来详细过程,希望能够让大家少走一些弯路。首先你需要准备CXF包,http://cxf.apache.org/download.html  可以下载最新的CXF包,这里我为了和spring3.0版本不冲突,选择的是2.2.10的低版本。下好后,在lib目录下有CXF和spring的包,因为我的项目中已经引入了spring框架,所以不需要重复导包了,我选择配置文件如下图    下面就可以写你的服务端接口了  
@WebService
public interface HelloWorld {
public String sayHello(String name);
}
然后实现这个接口@WebService(endpointInterface = "ws.HelloWorld",serviceName="HelloGT")public class HelloWorldImpl implements HelloWorld{@Overridepublic String sayHello(String name) {return "hello "+name;}}  其中 endpointInterface属性表明你的实现类是实现的那个接口 serviceName是指你的服务的名称实现类完成后,你需要在web-inf目录下放置webservice的配置文件,这个文件只能放在web-inf下,放在其他位置会报错。具体原因还不太清楚,希望有人能指点下。。。<?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:jaxws="http://cxf.apache.org/jaxws"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><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" /><bean id="service1" class="ws.impl.HelloWorldImpl"></bean><jaxws:endpoint id="hello" implementor="#service1" address="/HelloWorldService" /></beans> 头文件中的 
<span style="color:#ff0000;">http://cxf.apache.org/jaxws </span>
<span style="color:#ff0000;">http://cxf.apache.org/schemas/jaxws.xsd</span>
这两个地址是必须声明的
<pre name="code" class="html"> cxf.xml
cxf-extension-soap.xml
cxf-servlet.xml
这三个xml文件在 cxf-2.2.10.jar 这个jar包中 直接从jar包中拷贝到项目的META-INF文件夹下即可
接下来是web.xml中的配置
<pre name="code" class="html"><pre name="code" class="html"><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/beans.xml<!-- classpath*:applicationContext.xml --><!-- classpath*:spring/applicationContext*.xml --></param-value></context-param><listener>
在spring的监听中加入CXF的spring文件
<pre name="code" class="html">    <servlet><servlet-name>HelloWorldService</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>HelloWorldService</servlet-name><url-pattern>/services/*</url-pattern></servlet-mapping>
这个servlet是webservice的入口,url-pattern中的名称就是你访问webservice的路径
到现在配置全部都配置完成了,但是项目发布后,无法访问到服务,排查后发现是struts的拦截器把请求拦截了,所以需要重写struts的核心控制器。
<pre name="code" class="java">public class StrutsFilter extends StrutsPrepareAndExecuteFilter{private String[] excludedPageArray={"/services"};@Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {boolean isExcludedPage = false;String pagePath = ((HttpServletRequest)request).getServletPath();for (String page : excludedPageArray) {//判断是否在过滤url之外if(pagePath.equals(page)){isExcludedPage = true;break;}}if (isExcludedPage) {//在过滤url之外chain.doFilter(request, response);} else {//不在过滤url之外,判断session是否存在super.doFilter(request, response, chain);}}}
最后将web.xml中struts的控制器从StrutsPrepareAndExecuteFilter 这个类改成
StrutsFilter 你自己定义的这个类。
http://localhost:【端口号】/【项目名】/services/
访问,服务启动成功!
接下来是客户端
首先需要将你之前下载的CXF的包配置到环境变量中
变量名:CXF_HOME  变量值:D:\apache-cxf-2.2.10【这里填你的实际位置】
在path中最后加入 ;%CXF_HOME%/bin
接着打开cmd命令行,输入
wsdl2java -p ws.impl -d D:\src -server http://localhost:8080/XXXXX/services/HelloWorldService?wsdl
-P代表包名   -d 代表路径  -server  后面是服务路径

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: