1.新建Web工程,导入spring-3.2.8包与cxf2.7包,cxf2.x与cxf3.x差别不小,就像spring-2.x与spring3.x一样,cxf3.x把原cxf.jar分离出了N个jar包
cxf依赖包:
asm-3.3.1.jar
wsdl4j-1.6.3.jar
commons-logging-1.2.jar
cxf-2.7.17.jar
neethi-3.0.3.jar
xmlschema-core-2.1.0.jar
stax2-api-3.1.4.jar
woodstox-core-asl-4.4.1.jar
2.web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- cxf配置,添加CXFServlet -->
<servlet>
<servlet-name>CXF</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 捕获/doWS下的地址,wsdl地址要加上这个,开始没有这样做,浪费了一天的时间 -->
<servlet-mapping>
<servlet-name>CXF</servlet-name>
<url-pattern>/doWS/*</url-pattern>
</servlet-mapping>
</web-app>
注意:CXFServlet配置了url-pattern后,需要在wsdl地址中要加上这个配置:如要捕获的url的是/doWS/*,原wsdl地址是http://192.168.2.102:8080/spring_cxf/HelloWorld?wsdl,新的wsdl地址应该是http://192.168.2.102:8080/spring_cxf/doWS/HelloWorld?wsdl
3.spring容器:spring-config.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 导入cxf xml配置 -->
<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="hello" class="com.skymr.hello.ws.impl.HelloWorldBean"/>
<!-- 先导入命名空间和配置规范
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
-->
<!-- implementor
可以是类全名,这样的话它的实例是new的,不受spring容器管理,不好
可以引用spring容器里的bean
address
wsdl的相对路径,不能是绝对路径,绝对路径是随服务器地址变动的。
-->
<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />
</beans>
命名空间在cxf-2.x.jar包中的schema/jaxws.xsd找到;
规范地址在cxf-2.x.jar包中的spring.schemas文件中找到。
3.要暴露的接口
package com.skymr.hello.ws;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
public String sayHello(String name);
}
package com.skymr.hello.ws.impl;
import javax.jws.WebService;
import com.skymr.hello.ws.HelloWorld;
@WebService(endpointInterface="com.skymr.hello.ws.HelloWorld",serviceName="helloWorldws")
public class HelloWorldBean implements HelloWorld{
public String sayHello(String name) {
return "你好"+name+",欢迎进入Web Service学习课程";
}
}
5.测试
启动服务器,测试wsdl地址:http://localhost:8080/spring_cxf/doWS/HelloWorld?wsdl,使用wsdl2java工具生成客户端代码测试成功。