在各个系统交互领域,Web services逐渐成为主流。有两种主要方式来开发Web Services:Simple Object Access Protocol (SOAP)和Representational State Transfer (REST)
开发基于SOAP的Web Services需要很多的约束, 以便客户端和服务端交互信息,例如,使用Web Service Description Language (WSDL)来描述信息。还有很多WS的标准如WS-Security。
使用REST构架的服务被称为RESTful服务。这种架构利用简单的XML在HTTP协议上,就像网页一样发送请求,简化了基于SOAP架构开发。RESTful Web Services尤其适用于只需要提交和接受简单的XML信息。
接下来会介绍使用CXF框架来开发RESTful风格的Web Services。
简介
Java API for RESTful services
CXF JAX-RS实现
开发RESTful服务
REST也就是Representational State Transfer。REST并不特指一种技术,也不是一个标准,它仅仅是一个构架风格。REST 指的是一组架构约束条件和原则。满足这些约束条件和原则通过网络暴露资源给用户。事实上,WWW就是经典的REST架构风格。在服务器端,应用程序状态和功能可以分为各种资源。它向客户端公开。每个资源都使用 URI (Universal Resource Identifier) 得到一个惟一的地址。所有资源都共享统一的界面,以便在客户端和服务器之间传输状态。使用的是标准的 HTTP 方法,比如 GET、PUT、POST 和DELETE。客户端通过交换各种资源的表现体来查询或更新资源。资源的表现体包括HTML,XML,JSON等。客户端需要解析响应服务器的表现体。客户端和服务器之间的交互在请求之间是无状态的,无状态请求可以由任何可用服务器应答。
下面是一些RESTfu例子,提供雇员和部门的信息,并介绍客户端怎样访问这些服务
URI for the RESTful service—http://
• GET—获得deptname部门的所有员工信息
• POST—为deptname部门创建员工信息。
• DELETE—删除 deptname部门一名员工信息
URI for the RESTful service—http://
• GET—获得deptname部门名叫naveen的员工信息
• PUT—为deptname部门创建名叫naveen的员工信息
• DELETE—删除deptname部门名叫naveen的员工信息
下面是POST请求的例子http://
POST /department/deptname/employee HTTP/1.1
Content-Type: */*
Accept: application/xml
User-Agent: Apache CXF 2.2.2
Cache-Control: no-cache
Pragma: no-cache
Host: 127.0.0.1:9001
上一节,我们了解雇员POST请求。如果需要提供一种实现去识别雇员POST请求,我们应该做如下工作:
识别这是否一个HTTP POST请求。
将HTTP POST请求中的XML的内容转换为实现端所需要的格式,例如JAVA对象。
执行指定的操作,例如插入雇员信息到数据库。
以HTTP形式响应客户端,例如设置标志响应成功的HTTP状态200 ,并将响应转换到指定格式(XML或JSON),最后将其设置到HTTP Body。
依据需求,可能你要实现所有的HTTP方法,如GET,PUT,DELETE等。这不就是标准的RESTful JAVA开发模式吗?接着,Java API for RESTful Web services (JAX-RS)规范制定了开发RESTful的标准。
JAX-RS规范定义了创建RESTful服务的语法。JAX-RS使用annotations注解在实现RESTful的服务,使用annotations注解POJO将它暴露为RESTful资源。RESTful服务类中,通过URI(/category)和HTTP(GET, POST)方法注解方法。
@GET
@Path("/category/{id}")
public Category getCategory(@PathParam("id") String id)
实现JAX-RS的框架在运行的时候,通过映射HTTP请求到RESTful方法,负责调用正确的JAVA实现方法。JAX-RS规范提供这种映射的方法的算法。基础算法包括,判断JAVA资源类,判断HTTP URI请求,判断内容格式(例如application/xml),还有HTTP方法(例如GET)
JAX-RS规范提出如下要点:
POJO依赖性
为了暴露为资源,使用annotations注解POJO,
HTTP依赖性
RESTful资源暴露在HTTP中,规范中将HTTP 协议和JAX-RS API相对应映射。
格式独立性
API中提供嵌入式的方法,标准化添加HTTP内容的类型。例如,application/xml就是HTTP内容的类型中的一种。
容器独立性
可以在任何一种容器中部署实现JAX-RS规范的应用程序。
CXF实现了JAX-RS1.0规范,并提供了很多特性帮助开发者搭建企业级的RESTful服务。
下面是CXF框架提供创建RESTful服务的各种特性。
集成Spring
Spring框架已经变成事实上的构建企业级JAVA应用程序集成框架。CXF提供与Spring整合,简化了配置和部署RESTful应用程序。Spring提供依赖注入促进松散耦合,提供各种服务,像声明式事务管理。所有这些Spring提供的特性都可以被开发RESTful服务的CXF框架使用。
插入式数据绑定
数据库绑定就是映射HTTP请求,例如JSON或XML,到对应的JAVA对象。同样的,在发送HTTP响应之前,服务端的JAVA实现需要映射为客户端所需要的格式。通过提供数据库绑定组件,CXF在后台透明的处理映射。CXF支持各种数据绑定机制,如JAXB,JSON,XMLBean和Aegis。CXF允许指定特定的绑定机制。
客户端API
JAX-RS规范并没有提供客户端调用REST服务的API。CXF提供了这种API直接调用RESTful服务,也可以使用Spring框架配置到应用程序中。
安全
CXF可以使用Spring框架集成的声明式安全组件,按照应用程序的需要限制资源类和方法,而不必使用代码处理安全性问题。
过滤器
过滤器用来预处理或后处理信息。CXF可以创建和配置过滤器来审核信息,记录信息的日志,还有基于应用要求修改请求或响应。
CXF也运行开发者使用JAX-WS Provider和Dispatch API来创建RESTful服务。
本节将介绍使用JAX-RS实现的方法来开发RESTful服务,并执行CRUD操作。首先看一下Book Shop应用。
Book Shop应用是一款网络应用,提供技术书籍(JAVA或.NET)的分类。Book Shop让管理员为新的书籍创建分类,修改分类等。一旦这个分类存在,应用可以为这个分类添加新的书籍。
这个应用将提供如下方法:
创建分类
更新分类
删除分类
获取分类列表
获取特定分类
为特定分类添加书籍
获取特定分类中所有书籍
为了开发RESTful服务,需要做如下工作:
创建POJO类
为POJO类提供数据绑定
创建实现RESTful功能的服务类
创建调用RESTful服务的客户端
为了提供RESTful服务端和客户端通信,POJO需要转换为特定的格式,例如XML或JSON。为了实现这部分功能,需要一个数据绑定的组件来映射JAVA对象和XML(或者指定的格式)。
CXF使用JAXB作为默认的数据绑定组件。JAXB使用注解来定义JAVA对象和XML之间映射的关系。
在POJO类Category中,注解@XmlRootElement指定Category为XML的根元素。Category类的属性默认指定映射为@XmlElement。@XmlElement用来定义XML中的子元素。@XmlRootElement和@XmlElement允许自定义命名空间和XML中元素的名称。如果没有定义的话,JAXB在运行的时候默认的使用同样的属性名和类名来定义XML元素。
下面的XML请求表示了Category数据对象。
CategoryService的类声明上定义了@Path 和@Produces注解。@Path定义了URI路径,客户端可以通过这个路径访问到CategoryService对象。如@Path("/categoryservice"),那么URI请求,就可以是http://localhost:9000/categoryservice/。@Produces定义了服务类和方法生产内容的类型。如@Produces("application/xml"),那么CategoryService只会生产application/xml。
每个方法都和@Produces相关,它决定了这个方法生产什么样的响应。每个方法都有和HTTP方法映射的的注解,例如@GET 和@POST。方法中的@Path注解指定了该方法的URI。例如getCategory()方法上的@Path("/category/{id}"),可以使用http://localhost:9000/categoryservice/category/001,访问到category id 为001的分类。{id}就好比参数。接着,让我们看看@PathParam注解。@PathParam注解是用来给URI 路径一个模板变量,方法的输入参数。@PathParam("id") 就是URI中的 @Path ("/category/{id}")。
@GET
@Path("/category/{id}")
public Category getCategory(@PathParam("id") String id)
让我们看一种情况,客户端发送一个请求删除或者更新一个分类,但是这个分类并不存在,那么服务端需要返回正确的错误信息给客户端。
为了处理异常,JAX-RS提供了WebApplicationException,它继承自RuntimeException类。WebApplicationException可以使用HTTP状态代码或者javax.ws.rs.core.Response对象作为构造器。Response对象除了提供HTTP状态代码外,还可以提供用户容易识别的错误信息。
RESTful服务的异常处理可以分为如下几类:
实现类抛出带有HTTP错误代码的WebApplicationException异常。一般的,4XX定义了客户端错误,如错误请求;5XX定义了服务端错误,如服务器没有完成请求。
直接发回javax.ws.rs.core.Response对象,Response对象中包含了HTTP错误代码。
为了测试异常处理代码,启动CategoryServerStart类。在IE浏览器中输入
http://localhost:9000/categoryservice/category/011
由于IE不会显示自定义的错误信息,我们会看到HTTP 400 BAD Request。
如果你在Firefox或者Chrome,就会返回
在@Produces 和 @Consumes注解中添加application/json,指定CategoryService除了application/xml外,还接收和产生application/json。CXF运行时会处理HTTP JSON请求到JAVA对象的转换,还有JAVA对象到HTTP JSON响应的映射。
JAX-RS并不提供调用RESTful服务客户端。CXF框架提供了两种方式来创建客户端,这两种都可以使用Spring配置。
代理API
代理API允许你使用RESTful服务的资源类和接口。代理类是客户端直接调用接口方法,使用户不需要手工创建HTTP请求。将RESTful服务类传递给org.apache.cxf.jaxrs.client.JAXRSClientFactory类。一旦代理类创建好了,你可以直接使用RESTful服务接口类的任何方法。
CategoryService store = JAXRSClientFactory.create("http://
localhost:9000", CategoryService.class);
//Makes remote call to Category RESTFul service
store.getBooks("001");
HTTP客户端
使用org.apache.cxf.jaxrs.client.WebClient调用RESTful服务。本例中采用HTTP客户端。