CXF实现了Spring的完美结合,使webservice的开发更加方便,废话少说,马上开始
环境:JDK1.5,tomcat5.5,myeclipse7.5
开始之前,先从apache下载CXF.
1.先创建一个web project工程CXF_project,把从上面从apahce上下载的CXF的lib目录下所有的jar导入工程
2.接口的创建
package com.cxf;
import javax.jws.WebService;
@WebService //1
public interface IHelloWorld {
public String sayHello(String name);
}
实现类的创建
package com.cxf;
import javax.jws.WebService;
@WebService //2
public class HelloWorld implements IHelloWorld {
public String sayHello(String name) {
return "Hello, "+(name==null?"":name.trim());
}
public String sayGoodBy(String name) {
return "Goodbye, "+(name==null?"":name.trim());
}
}
3.新建spring配置文件applicationContext.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-2.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- Import apache CXF bean definition -->
<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"/>
<!-- Define HelloWorld Bean -->
<bean id="helloWorld" class="com.cxf.HelloWorld"></bean>
<!-- Define HelloWorld service-->
<jaxws:server id="helloWorldService"
serviceClass="com.cxf.IHelloWorld"
address="/HelloWorld">
<jaxws:serviceBean>
<ref bean="helloWorld"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
上面显示,spring通过jaxws:server发布webservice服务端应用接口,serviceClass表示发布的接口,address表示接口的地址,如http://localhost:8080/CXF/services/HelloWorld
4.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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<display-name>CXF Servlet</display-name>
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
上面除了定义spring,还定义了CXF的访问路径
目前为止,开发webservice的步聚完毕,只需把CXF_project发布到tomcat并重启server,访问
http://localhost:8080/CXF/services/HelloWorld?wsdl,就可以看到相关的信息,webservice发布成功.
5.下面是编写客户端程序,所对于xfire,CXF+Spring更加灵活,以bean的方式调用,客户端的webservice配置文件如下
<?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-2.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- Import apache CXF bean definition -->
<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="helloWorld"
serviceClass="com.cxf.IHelloWorld"
address="http://localhost:8080/CXF/services/HelloWorld">
</jaxws:client>
</beans>
如上所示,spring通过jaxws:client形式定义了客户端的bean,通过它可以调用服务端的方法
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cxf.IHelloWorld;
public class HelloWorldClient {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextClient.xml");
IHelloWorld helloWorld = (IHelloWorld)context.getBean("helloWorld");
System.out.println(helloWorld.sayHello("Tom"));
}
}
运行上面的程序,输出:Hello, Tom.
总结:
所对于axis2,CXF支持Spring,以配置文件形式定义webservice,而客户端访问程序简单灵活.