开发环境: myeclipse8.5 + tomcat6.0 + jdk1.6
CXF 版本: apache-cxf-2.3.5.zip (在这里介绍一个下载包的网址给大家: http://www.jar114.com )
把 apache-cxf-2.3.5.zip 解压出来后,如下图所示目录:(不做解析了)
新建一个 web 项目,命名为: CXFServer
建一个包: com.server.dao
在该包下面建一个接口,命名为: Hello ,具体代码如下:
package com.server.dao;
import javax.jws.WebService;
@WebService
public interface Hello {
public String hello(String username);
}
新建一个包: com.server.service
在该包下建一个现实类,命名为: HelloImpl 具体代码如下:
package com.server.service;
import javax.jws.WebService;
import com.server.dao.Hello;
@WebService
public class HelloImpl implements Hello {
public String hello(String username) {
System.out.println("server is called!");
return "sayHello" + username;
}
}
既然要用到 Spring ,那么就少不了在 web.xml 里面配置 Spring 的过滤器!
在 web.xml 配置 Spring ,如下所示:
<? 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" >
< welcome-file-list >
< welcome-file > index.jsp </ welcome-file >
</ welcome-file-list >
< servlet >
< description > apache cxf 配置 webservice 服务 </ description >
< servlet-name > cxf </ servlet-name >
< servlet-class > org.apache.cxf.transport.servlet.CXFServlet </ servlet-class >
< load-on-startup > 1 </ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name > cxf </ servlet-name >
< url-pattern > /services/* </ url-pattern >
</ servlet-mapping >
< listener >
< description > spring 的监听 </ description >
< listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
</ listener >
< context-param >
< description > spring 的配置文件加载路径 </ description >
< param-name > contextConfigLocation </ param-name >
< param-value > classpath *:applicationContext*.xml </ param-value >
</ context-param >
</ web-app >
项目增加 Spring 功能后,那么就要配置 Spring 文件了!
Spring 配置文件如下 ;
<? 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" />
< jaxws:endpoint id = "service"
implementor = "com.server.service.HelloImpl" address = "/webserviceHello" />
</ beans >
< 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" />
这 3 句是固定的一个配置!
< jaxws:endpoint id = "service"
implementor = "com.server.service.HelloImpl" address = "/webserviceHello" />
id :指在 spring 配置的 bean 的 ID.
Implementor: 指明具体的实现类 .
Address: 指明这个 web service 的相对地址
把项目发布到 tomcat 上,启动 tomcat ,在浏览器打开 http://localhost:8080/CXFServer/services/webserviceHello?wsdl 能现实如下界面,证明服务器已经成功发布了!
有的同学可能会对这个访问地址存在疑问:
http://localhost:8080/CXFServer/services/webserviceHello?wsdl
问什么是这样子的访问地址呢?
http://localhost:8080/CXFServer 是本项目的访问地址
services 是由于 web.xml 配置所得:
< servlet-mapping >
< servlet-name > cxf </ servlet-name >
< url-pattern > /services/* </ url-pattern >
</ servlet-mapping >
webserviceHello 是由于 Spring 配置文件中的 address 属性所得:
< jaxws:endpoint id = "service"
implementor = "com.server.service.HelloImpl" address = "/webserviceHello" />
现在服务器发布成功了,接着就写客户端程序了!
新建一个 web 项目,命名为 ;CXFClient
建一个包 ;com.server.dao (这个接口的包名要与服务器接口包名一样)
建一个接口: Hello (最好与服务器的接口名字一样)
代码如下:
package com.server.dao;
import javax.jws.WebService;
@WebService
public interface Hello {
// 这里的方法名必须与服务器接口的方法一样
public String hello(String username);
}
新建一个测试类: Test
代码如下:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "classpath:applicationContext*.xml" );
Hello service = (Hello) context.getBean( "webServiceClient" );
System. out .println(service.hello( " 和谐 dota" ));
}
}
代码基本上完成了,现在为项目增加 Spring 功能, 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" >
< welcome-file-list >
< welcome-file > index.jsp </ welcome-file >
</ welcome-file-list >
< servlet >
< description > apache cxf 配置 webservice 服务 </ description >
< servlet-name > cxf </ servlet-name >
< servlet-class > org.apache.cxf.transport.servlet.CXFServlet </ servlet-class >
< load-on-startup > 1 </ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name > cxf </ servlet-name >
< url-pattern > /services/* </ url-pattern >
</ servlet-mapping >
< listener >
< description > spring 的监听 </ description >
< listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
</ listener >
< context-param >
< description > spring 的配置文件加载路径 </ description >
< param-name > contextConfigLocation </ param-name >
< param-value > classpath *:applicationContext*.xml </ param-value >
</ context-param >
</ web-app >
对应的 Spring 配置文件如下所示:
<? 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" />
< jaxws:client id = "webServiceClient"
address = "http://localhost:8080/CXFServer/services/webserviceHello"
serviceClass = "com.server.dao.Hello" />
</ beans >
最后执行测试类: Test 的代码,控制台会打印出: sayHello 和谐 dota
到此, CXF 与 Spring 整合已经完成了!!希望能给你带来一点帮助!!
注解: Spring 配置文件放在 src 目录下就可以了!