开发环境:Eclipse indigo + Tomcat6.0 + JDK1.6 + CXF2.5
1、Eclipse新建一个Dynamic Web Project工程,创建时即可以选择运行环境,如Tomcat6.0、CXF2.5。
建好后在工程属性Properties->Java Build Path->Source->Default output folder里把:项目名/build/classes修改为:项目名/WebContent/WEB-INF/classes(只是习惯,也可不改)
2、创建后会包含所有CXF库,如果创建时没有配置、选择CXF,可以从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)
1、Eclipse新建一个Dynamic Web Project工程,创建时即可以选择运行环境,如Tomcat6.0、CXF2.5。
建好后在工程属性Properties->Java Build Path->Source->Default output folder里把:项目名/build/classes修改为:项目名/WebContent/WEB-INF/classes(只是习惯,也可不改)
2、创建后会包含所有CXF库,如果创建时没有配置、选择CXF,可以从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)
3、编辑web.xml
- 在web.xml文件中添加以下servlet配置:
-
<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 >
< load-on-startup > 1 </ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name > CXFServlet </ servlet-name >
< url-pattern > /services/* </ url-pattern >
</ servlet-mapping >
- 添加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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
default-autowire ="byName" default-lazy-init ="true" >
< description > 基于Apache CXF的Web Service配置文件 </ description >
< import resource ="classpath:META-INF/cxf/cxf.xml" />
< import resource ="classpath:META-INF/cxf/cxf-servlet.xml" />
< import resource ="classpath:META-INF/cxf/cxf-extension-soap.xml" />
< import
resource ="classpath:META-INF/cxf/cxf-extension-javascript-client.xml" />
< bean id ="helloWorldImpl" class ="com.lbg.ws.test.impl.HelloWorld" />
< jaxws:endpoint id ="helloWorld" implementor ="#helloWorldImpl"
address ="/HelloWorld" />
</ beans >
- 建立webservice的接口与实现:
package com.lbg.ws.test;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService(name = " HelloWorld " )
public interface IHelloWorld {
@WebMethod(operationName="sayHello")
public String sayHello();
}
package com.lbg.ws.test.impl;
import javax.jws.WebService;
import com.lbg.ws.test.IHelloWorld;
@WebService(endpointInterface = " com.lbg.ws.test.IHelloWorld " )
public class HelloWorld implements IHelloWorld {
public String sayHello() {
return "HelloWorld!";
}
}
- 注意:@WebService表示该接口是一个WebService服务,@WebResult(name="response")表示在返回的SOAP消息中回应的标签名称;如果sayHello有参数,则需要使用@WebParam(name="param"),需包含 import javax.jws.WebParam
- 把项目部署到tomcat或其它j2ee容器上启动,成功信息如下:
2008 - 6 - 30 21 : 16 : 57 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://impl.test.ws.lbg.com/}HelloWorldService from class com.lbg.ws.test.IHelloWorld
2008 - 6 - 30 21 : 16 : 57 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be /HelloWorld
好了,那么样才能够看到wsdl文档呢?关键就在web.xml配置servlet那里,
< servlet-mapping >
< servlet-name > CXFServlet </ servlet-name >
< url-pattern > /services/* </ url-pattern >
</ servlet-mapping >