spring集成CXF服务端

本项目使用maven构建,根据查询资料,并结合自身遇到的问题整理

1.pom文件配置(这里只列出了cxf的jar依赖,其余jar包依赖详见项目)

    <!-- start  cxf jar -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.7</version>
        </dependency>

        <dependency>
            <groupId>org.apache.ws.xmlschema</groupId>
            <artifactId>xmlschema-core</artifactId>
            <version>2.2.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.neethi</groupId>
            <artifactId>neethi</artifactId>
            <version>3.0.3</version>
        </dependency>
        <!-- end  cxf jar -->

   <dependency>
        <groupId>org.codehaus.woodstox</groupId>
        <artifactId>woodstox-core-asl</artifactId>
        <version>4.4.0</version>
    </dependency>
    
    <dependency>
        <groupId>org.codehaus.woodstox</groupId>
        <artifactId>stax2-api</artifactId>
        <version>3.1.4</version>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>
    
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.7</version>
    </dependency>

2.web.xml配置(这里只列出了关于webservice.xml的文件)

 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:webservice.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

 //加载cxf核心的servlet

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

    <servlet-mapping>
        <servlet-name>wps</servlet-name>
        <url-pattern>/server/*</url-pattern> //注:将包含server的请求地址都交给cxf处理
    </servlet-mapping>

3.webservice.xml文件的配置

         <!-- 引入cxf的配置文件 -->
        <import resource="classpath:META-INF/cxf/cxf.xml"></import>
        <import resource="classpath:META-INF/cxf/cxf-servlet.xml"></import>
       <!-- 扫描的基础包名 -->
        <context:component-scan base-package="com.wps.webservice.server"></context:component-scan>
        <!-- 发布的webservice的地址  implementor :表示访问的实现类的文件,可以直接写  包名+文件名  ;address:发布的地址 -->
       <jaxws:endpoint   implementor="#helloWordServiceImpl"
                       address="/hello"></jaxws:endpoint>

4.接口文件与实现类文件的配置

  @WebService
   public interface HelloWordService {
      public String hello(String name);
   }

 

  //将此类交给spring容器管理,名称是:helloWordServiceImpl

   @Component("helloWordServiceImpl")

  //标注此类是一个webservice,endpointInterface 表示接口的路径

  @WebService(endpointInterface = "com.wps.webservice.server.HelloWordService",
            serviceName = "helloWordServer")
  public class HelloWordServiceImpl implements HelloWordService {

 //标注webservice提供的方法,可以使用 @WebParam(name="arg0")来接收参数,注:参数的名称要和客户端写的参数名称一致,否则报错

    @WebMethod
    public String hello(@WebParam(name="arg0")String name) {
        System.out.println("webservice");
        return "你好=" + name;
    }
}

  5.启动tomcat,访问http://localhost:8080/bdp/server;即可看到发布的server接口;注:bdp为项目名称

注意事项:1.需要将webservice.xml单独加载,否则会找不到访问http://localhost:8080/bdp/server后,会出现找不到server的问题;

                    2.需要在spring容器启动成功之后加载cxf,否则客户端调用会出现Cannot create a secure XMLInputFactory 异常 ,如下:

                       @Component
                       public class SysStartListener implements ApplicationListener<ApplicationEvent>{
                           @Override
                            public void onApplicationEvent(ApplicationEvent event) {
                                 if(event instanceof ContextRefreshedEvent){
                                       //注:为了使客户端能够调用cxf,在系统启动成功后,加载cxf
                                        Properties props = System.getProperties();
                                        props.setProperty("org.apache.cxf.stax.allowInsecureParser", "1");
                                        props.setProperty("UseSunHttpHandler", "true");
                                  }
                            }
                        }