您的位置:首页 > 编程语言 > Java开发

ssh 整合 cxf

2015-03-25 12:21 204 查看
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

 

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