CXF是apache旗下的开源框架,由Celtix + XFire这两门经典的框架合成,是一套非常流行的web service框架。
它提供了JAX-WS的全面支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用,同时它能与spring进行完美结合。
在apache cxf官网提供了cxf较全面的帮助文档,英语教好的童鞋可以到这个地址学习:http://cxf.apache.org/docs/index.html
下面就以官网教程为例,简单介绍下cxf的使用。
1、依赖的jar包
去官网下载cxf压缩文件:http://cxf.apache.org/download.html
解压后,把apache-cxf-2.4.1\lib目录下的jar包引用到java项目中
2、JAX-WS简单实例
首先编写一个ws接口:
@WebService public interface HelloService { public String sayHi(String text); public String getUser(User user); public List<User> getListUser(); }
需要在接口头部注明一个"WebService"注解,表示这是一个webservice。
至于User类则是一个序列化的javabean(在对象传输过程建议尽量序列化,熟悉java io的朋友应该清楚这点):
public class User implements Serializable{ private static final long serialVersionUID = 1001881900957402607L; private Integer id; private String name; getter,setter... }
然后编写接口的实现类:
@WebService(endpointInterface = "com.bless.ws.HelloService", serviceName = "HelloService",portName="HelloServicePort") public class HelloServiceImpl implements HelloService { @Override public String sayHi(String text) { System.out.println("sayHi called..."); return "Hi :" + text; } @Override public String getUser(User user) { System.out.println("sayUser called..."); return "User:[id=" + user.getId() + "][name=" + user.getName() + "]"; } @Override public List<User> getListUser() { System.out.println("getListUser called..."); List<User> lst = new ArrayList<User>(); lst.add(new User(2, "u2")); lst.add(new User(3, "u3")); lst.add(new User(4, "u4")); lst.add(new User(5, "u5")); lst.add(new User(6, "u6")); return lst; } }
此时注解WebService内还有三个属性:
endpointInterface表示webservice接口名,因为一个类可以继承多个接口,你必须指明哪个是webservice接口
serviceName:表示当前webservice的别名
portName:表示当前webservice的端口名
这些属性定义好之后,在wsdl中是能看到的,如果不定义,cxf会配置默认的别名和端口名
最后一步部署webservice:
public class Server { protected Server() throws Exception { // START SNIPPET: publish System.out.println("Starting Server"); HelloServiceImpl implementor = new HelloServiceImpl(); String address = "http://localhost:8111/helloWorld"; Endpoint.publish(address, implementor); // END SNIPPET: publish } public static void main(String[] args) throws Exception { new Server(); System.out.println("Server ready..."); Thread.sleep(5 * 60 * 1000); System.out.println("Server exiting"); System.exit(0); } }
web service是需要部署到服务器的,通过Endpoint.publish方法部署的话,我估计是部署到jetty上的,具体神马情况,因为个人经验不足在这不胡说了。通过运行main函数就可以启动服务了,检验服务是否启动,可以访问如下地址:http://localhost:8111/helloWorld?wsdl,如果能显示正确的xml信息则表示服务启动成功。
最后写一个客户端程序调用web service:
public final class Client { private static final QName SERVICE_NAME = new QName("http://ws.bless.com/", "HelloService"); private static final QName PORT_NAME = new QName("http://ws.bless.com/", "HelloServicePort"); private Client() { } public static void main(String args[]) throws Exception { Service service = Service.create(SERVICE_NAME); // Endpoint Address String endpointAddress = "http://localhost:8111/helloWorld"; // Add a port to the Service service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress); HelloService hw = service.getPort(HelloService.class); System.out.println(hw.sayHi("World")); System.out.println(hw.getUser(new User(1, "kaka"))); for(User user : hw.getListUser()){ System.out.println("List User [id:"+user.getId()+"][name:"+user.getName()+"]"); } } }
测试:
首先启动server,如果没问题的话,再启动clien,大家可以看控制台的效果。
3、CXF与spring整合:
熟悉spring的朋友应该知道spring的IOC管理对象非常强大,那么cxf与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="helloWorld" implementor="com.bless.ws.HelloServiceImpl" address="http://localhost:8080/webservice/helloService" /> <jaxws:client id="helloClient" serviceClass="com.bless.ws.HelloService" address="http://localhost:8080/webservice/helloService" /> </beans>
大家可以通过java代码测试(测试时把上一步配置的beans.xml文件放在src根下面):
public class SpringTest { public static void main(String[] args) { // START SNIPPET: client ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); HelloService client = (HelloService)context.getBean("helloClient"); String response = client.sayHi("Joe"); System.out.println("Response: " + response); System.exit(0); // END SNIPPET: client } }
如果是web项目,那么你需要配置web.xml文件:
<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>CxfDemo</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/beans.xml</param-value> </context-param> <!--Spring ApplicationContext 载入 ,必须--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring 刷新Introspector防止内存泄露 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</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>/webservice/*</url-pattern> </servlet-mapping> </web-app>