一、项目环境

    SpringMVC框架 版本:4.3.1

    CXF  版本:3.1.6

二、问题现象

   配置好相关配置文件,验证webService是否发布成功,访问:

    http://localhost:8080/CXF_Spring/webservice/HelloWorld?wsdl

   页面提示:

    No service was found

   后台提示:

   WARNING: Can't find the the request for     http://localhost:8080/CXF_Spring/webservice/HelloWorld's Observer 

三、问题分析

WARNING: Can't find the the request for http://localhost:8080/CXF_Spring/webservice/HelloWorld's Observer 

   出现上述提示则意味着CXF的核心控制器没起作用,CXF的核心控制器要依赖Spring的ContextLoaderListner,而SpringMVC用的是DiapacherServlet的配置方式。问题搞清楚了,那么解决方案就很明显了。

四、解决方案(其实也是整合SpringMvc和cxf的完整教程)

    拆分配置文件,spring-servlet.xml(DiapacherServlet配置方式)中配置Controller组件,root-context.xml(ContextLoaderListner配置方式)中配置普通bean和CXF。

详细配置如下(只展示与解决这个问题有关的配置):

web.xml

<listener>
<description>Define spring4.x listener.</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
<description>Define applicationContext.xml location.</description>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:root-context.xml
</param-value>
</context-param>


<servlet>
<description>SpringMVC dispatcher servlet.</description>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>Define springMVC configuration location.</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>

spring-servlet.xml(DiapacherServlet配置方式)

该配置文件没有与CXF有关的配置,略过

root-context.xml(ContextLoaderListner配置方式)

在该配置文件中首先要额外加上这些链接:xmlns:jaxws=”http://cxf.apache.org/jaxws"

xsi:schemaLocation=” http://cxf.apache.org/jaxws   http://cxf.apache.org/schemas/jaxws.xsd"


<!-- 引cxf的一些核心配置 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<!-- cxf 3.1.6版本不存在如下两个xml,可能在较早版本中会存在且需要需要
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
-->

<bean id="hello" class="com.dx.webservice.HelloWorldImpl" />
<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />

pom.xml

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.0-alpha4</version>
</dependency>

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.1.6</version>
</dependency>

HelloWorld.java

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
//@WebService(targetNamespace="tallent")
//如果不写targetNamespace的值时,默认是包反转,比如服务器项目中包是com.gstd.hw,那么默认值为hw.gstd.com,如果在另外
//的项目客户端中调用,则创建接口类HelloWorld时,类名可以不一样,但是targetNamespace必须一样。不然调用不成功!最好自己定义一个名称
@SOAPBinding(style = Style.RPC)
public interface HelloWorld {
String sayHi(String text);
}

HelloWorldImpl.java

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService(endpointInterface = "com.dx.webservice.HelloWorld")////这里指定服务的接口类的路径,也可以不写
@SOAPBinding(style = Style.RPC)
public class HelloWorldImpl implements HelloWorld{

@Override
public String sayHi(@WebParam(name = "text")String text) {
System.out.println("sayHi called");
return "Hello " + text;
}

}

上述配置完成后,访问 http://localhost:8080/CXF_Spring/webservice/HelloWorld?wsdl即可看到发布的webservice的xml了。

访问http://localhost:8080/CXF_Spring/webservice/可查看发布的接口列表。