先建一个web项目,CXF_Spring。
配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<!-- 加载Spring容器配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置Spring容器加载配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-server.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
配置applicationContext-sercer.xml文件
<?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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.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="userServiceBean" class="com.gg.service.ComplexUserService"/>
<jaxws:server id="userService" serviceClass="com.gg.service.IComplexUserService" address="/Users">
<jaxws:serviceBean>
<!-- 要暴露的 bean 的引用 -->
<ref bean="userServiceBean"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
建一个webservice接口
package com.gg.service;
import javax.jws.WebService;
@WebService
public class ComplexUserService implements IComplexUserService {
@Override
public String sayHello(String userName) {
System.out.println("HelloWorldServiceImpl.sayHello("+userName+")");
return "Hello,"+userName;
}
}
实现该接口
package com.gg.service;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface IComplexUserService {
public String sayHello(@WebParam(name="userName") String userName);
}
最后启动tomcat服务器。访问http://localhost:8080/CXF_Spring/Users?wsdl
遇到的问题:启动Tomcat一直说我的配置文件applicationContext-server.xml有问题。经过多次修改还是提示同个问题。
调试bug真的好他妈的痛苦,最终在项目classes文件夹中发现生成的applicationContext-server.xml文件的修改日期不变,原来这个文件一直没重新编译成功。
解决办法:clean一个该项目。