开发环境:Eclipse3.4.2 + Tomcat6.0 + JDK1.6 + CXF2.2.3
1、Eclipse新建一个Dynamic Web Project工程CxfTest,我习惯把WebContent改为WebRoot,建好后在工程属性Properties->Java Build Path->Source->Default output folder里把CxfTest/build/classes修改为CxfTest/WebRoot/WEB-INF/classes
2、从cxf文件夹下的lib文件夹中复制cxf.jar、spring-core.jar、spring-web.jar、spring-beans.jar、spring-context.jar、commons-logging.jar、FastInfoset.jar、needhi.jar、wsdl4j.jar、XmlSchema.jar,在Eclipse->CxfTest->WebRoot->WEB-INF->lib点击右键选择粘贴(当然,也可以直接复制到该目录下然后刷新lib,同时,我一般都是从单独下载的Spring中得到相关的spring的jar包)
3、编辑WEB-INF/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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>CxfTest</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/service-beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
4、创建WEB-INF/service-beans.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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="helloWorld" class="cc.mysite.cxf.test.HelloWorld" />
<jaxws:endpoint id="helloWorldService" implementor="#helloWorld" address="/helloworld" />
</beans>
注意:该文件中的三行<import.../>不能少了,否则加载的时候会出错的:Caused by: java.net.MalformedURLException: no protocol: /helloworld
5、创建一个服务接口 IHelloWorld.java:
package cc.mysite.cxf.test;
import javax.jws.WebService;
import javax.jws.WebResult;
@WebService
public interface IHelloWorld {
@WebResult(name="response")
public String sayHello();
}
注意:@WebService表示该接口是一个WebService服务,@WebResult(name="response")表示在返回的SOAP消息中回应的标签名称;如果sayHello有参数,则需要使用@WebParam(name="param"),需包含 import javax.jws.WebParam,如:
public String sayHello(
@WebParam(name="param1")String param1,
@WebParam(name="param2")String param2);
6、创建服务实现文件 HelloWorld.java:
package cc.mysite.cxf.test;
public class HelloWorld implements IHelloWorld {
public String sayHello() {
return "Hello, world!";
}
}
7、假如在Tomcat中部署后的访问地址是: http://localhost:8080/cxf,则在浏览器中使用如下地址做测试:
1> http://localhost:8080/cxf 能看到所有服务
Available SOAP services:
IHelloWorld
|
Endpoint address: http://localhost:8080/cxf/helloworld WSDL : {http://test.cxf.saile.cc/}HelloWorldService Target namespace: http://test.cxf.saile.cc/ |
2> http://localhost:8080/cxf/helloworld?wsdl 能看到生成的WSDL文件
3> http://localhost:8080/cxf/helloworld/sayHello 能访问sayHello服务