首先:什么是CXF?为什么要用CXF?

CXF 包含了大量的功能特性,但是主要集中在以下几个方面:支持 Web Services 标准:CXF 支持多种 Web Services 标准,包含 SOAP、Basic Profile、WS-Addressing、WS-Policy、WS-ReliableMessaging 和 WS-Security。Frontends:CXF 支持多种“Frontend”编程模型,CXF 实现了 JAX-WS API (遵循 JAX-WS 2.0 TCK 版本),它也包含一个“simple frontend”允许客户端和 EndPoint 的创建,而不需要 Annotation 注解。CXF 既支持 WSDL 优先开发,也支持从 Java 的代码优先开发模式。容易使用: CXF 设计得更加直观与容易使用。有大量简单的 API 用来快速地构建代码优先的 Services,各种 Maven 的插件也使集成更加容易,支持 JAX-WS API ,支持 Spring 2.0 更加简化的 XML 配置方式,等等。支持二进制和遗留协议:CXF 的设计是一种可插拨的架构,既可以支持 XML ,也可以支持非 XML 的类型绑定,比如:JSON 和 CORBA。

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(, "u2"));
lst.add(new User(, "u3"));
lst.add(new User(, "u4"));
lst.add(new User(, "u5"));
lst.add(new User(, "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( * * );
System.out.println("Server exiting");
System.exit();
}
}

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(, "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();
// 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></load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping> </web-app>

CXF框架入门实例的更多相关文章

  1. CXF框架入门(重点)

    l CXF是一个开源的webservice框架 l CXF支持的协议:SOAP.XML/HTTP等 l CXF可以很好的和spring集成 l CXF可以部署到tomcat.jboss.jetty等服 ...

  2. scrapy爬虫框架入门实例(一)

    流程分析 抓取内容(百度贴吧:网络爬虫吧) 页面: http://tieba.baidu.com/f?kw=%E7%BD%91%E7%BB%9C%E7%88%AC%E8%99%AB&ie=ut ...

  3. EXTJS框架-入门实例

    extjs框架是一个JavaScript框架,可以渲染出丰富的控件 实例: 代码: <html> <head> <title>test</title> ...

  4. Python之Scrapy爬虫框架 入门实例(一)

    一.开发环境 1.安装 scrapy 2.安装 python2.7 3.安装编辑器 PyCharm 二.创建scrapy项目pachong 1.在命令行输入命令:scrapy startproject ...

  5. cxf 框架 webservice

    cxf 内置了一个web服务器 cxf简单入门实例 package test; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import c ...

  6. 项目一:第六天 WebService写接口 和CXF框架

    1 课程计划 1. webService入门(了解) 2. 基于jdk1.7开发webservice服务(了解) 3. Apache CXF框架入门(掌握) 4. 基于CXF框架搭建CRM系统(掌握) ...

  7. SpringMVC 框架系列之初识与入门实例

    微信公众号:compassblog 欢迎关注.转发,互相学习,共同进步! 有任何问题,请后台留言联系! 1.SpringMVC 概述 (1). MVC:Model-View-Control Contr ...

  8. Webservice与CXF框架快速入门

    1. Webservice Webservice是一套远程调用技术规范 远程调用RPC, 实现了系统与系统进程间的远程通信.java领域有很多可实现远程通讯的技术,如:RMI(Socket + 序列化 ...

  9. Python爬虫Scrapy框架入门(0)

    想学习爬虫,又想了解python语言,有个python高手推荐我看看scrapy. scrapy是一个python爬虫框架,据说很灵活,网上介绍该框架的信息很多,此处不再赘述.专心记录我自己遇到的问题 ...

随机推荐

  1. asp.net 从客户端中检测到有潜在危险的 Request.Form 值错误解

    从客户端(ftbContent="<P><A href="http://l...")中检测到有潜在危险的 Request.Form 值. 说明: 请求验 ...

  2. for update和for update nowait的区别和使用

    首先,for update 和for update nowait 是对操作的数据行进行加锁,在事务提交前防止其他操作对数据的修改. for update 和for update nowait主要区别在 ...

  3. OC - 16.大文件下载

    大文件下载注意事项 若不对下载的文件进行转存,会造成内存消耗急剧升高,甚至耗尽内存资源,造成程序终止. 在文件下载过程中通常会出现中途停止的状况,若不做处理,就要重新开始下载,浪费流量. 大文件下载的 ...

  4. Linux下面/usr/local和opt目录

    1./opt This directory is reserved for all the software and add-on packages that are not part of the ...

  5. 最短路径算法——Dijkstra,Bellman-Ford,Floyd-Warshall,Johnson

    根据DSqiu的blog整理出来 :http://dsqiu.iteye.com/blog/1689163 PS:模板是自己写的,如有错误欢迎指出~ 本文内容框架: §1 Dijkstra算法 §2 ...

  6. Qt中如何在QCursor移动的时候不触发moveEvent

    有时候有这样的需求,比如想对全局光标进行一次setPos(),但这个时候又不想触发消息队列触发mouseMoveEvent,这个时候就可以这么做. myWidget->clearFocus(); ...

  7. jquery mobile listview列表属性(搜索自动填充检索效果)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  8. YesFInder - Web File Manager 网页文件管理系统

    开发原由: 原来想找一下实现可视化图片上传程序,先找了ckfinder,发现居然是收费的,而且用起来也不顺手,于是想能不能自己写一个.想到这就立即动手,花2天时间完成初步功能,然后再花了3天完善.目前 ...

  9. php 使用phpqrcode类生成带有logo的二维码 使logo不失真(透明)

    在开发中 发现phpqrcode类在加入logo时,如果 logo 是 png 图像带有透明区域时,二维码上都无法正常完美的显示出来 解决方法便是:修改phpqrcode文件中的 QRimage类下的 ...

  10. frame与iframe的区别?

    1.frame不能脱离frameSet单独使用,iframe可以 2.frame不能放在body中,否则不能正常显示 3.嵌套在frameSet中的iframe必需放在body中,不嵌套在frameS ...