S2S4H3整合cxf

准备:

我是已经有了一个S2S4H3的整合项目由于需要整合了cxf估计看见这篇文档的人都是知道cxf框架是干什么的吧,

为了发布WebService,要做一些前期准备,当然S2S4H3的项目肯定要有了,在这里我就不解释SSH的整合啦,关注我的下篇文档吧。

     准备cxfjar包:

我用的是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 的默认过滤器默认是过滤捕捉所以请求的,所以需要自定义Filtercxf放行

                       <servlet>

                           <servlet-name>cxf</servlet-name>

                           <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

                           <load-on-startup>30</load-on-startup>

                       </servlet>



<!-- 需要注意:cxfServletstruts2URL不能都写/*,需要区分,在自定义Filter中对cxf放行,不然请求时会在struts2里出错-->



                       <servlet-mapping>

                            <servlet-name>sshserver </servlet-name>        

                            <url-pattern>/sshserver/*</url-pattern>

                       </servlet-mapping>

            


<!-- 配置struts2filter:自定Filtercxf放行)-->



                   <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源文件 -->

            <importresource="classpath:META-INF/cxf/cxf.xml" />

            <importresource="classpath:META-INF/cxf/cxf-extension-soap.xml" />

            <importresource="classpath:META-INF/cxf/cxf-servlet.xml" />

        <!--布服-->

            <jaxws:endpointid="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;

importorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;



/**

 * <p>自定义Filter cxf放行</p>

*

 */


public class LetCxfGoFilter extends StrutsPrepareAndExecuteFilter {

@Override

public void doFilter(ServletRequest arg0, ServletResponsearg1,FilterChain arg2) throws IOException, ServletException {     

HttpServletRequestrequest = (HttpServletRequest) arg0;


//如果路径包含sshserver则放行。

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/sshserver/ServerPublish?wsdl