至于deleteItem、findItemById和findAllItems服务,REST方法使用的消息交换协议会有些不同,这些操作实际上不需要上传XML到服务器,它们最多只需要传给服务器一个Id号,像findAllItems根本就不要传给服务器任何输入。在这些情况下,URI和HTTP谓词就能提供服务器需要的所有信息。下表列出了REST请求的一些URI实例:
| 谓词 | URI示例 | 操作 |
| DELETE | http://localhost/SoaBookREST/itemService/14 | 删除第14号商品 |
| GET | http://localhost/SoaBookREST/itemService/14 | 获取第14号商品 |
| GET | http://localhost/SoaBookREST/itemService | 获取所有商品 |
您会从上表看到,HTTP请求由谓词加URI构成,它清楚地告诉服务器它要求哪个服务。请您注意,上面的URI包含了一些额外数据(比如Id号),因此您需要web描述文件中配置相应的URL映射模式,而不是针对某个具体的URL进行配置,web.xml的示例配置如下:
代码清单12— web.xml中servlet的映射配置
<servlet-mapping>
<servlet-name>ItemService</servlet-name>
<url-pattern>/itemService/*</url-pattern>
</servlet-mapping>
下面是最后两个操作的服务实现代码:
代码清单13— REST风格读取服务的实现
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
try{
if (request.getPathInfo()==null){
//findAllItems
ItemList itemList = new ItemList();
itemList.setList(new ArrayList());
//retrieve all items
...
itemList.getList().add(...);
...
//Send the XML message to the client
JAXBContext jaxbContext = JAXBContext.newInstance(ItemList.class, Item.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);
marshaller.marshal(itemList, response.getOutputStream());
} else {
//findItemById
int id = (new Integer(request.getPathInfo().substring(1))).intValue();
//retrieve item by id (e.g. from a database)
Item item = ...
JAXBContext jaxbContext = JAXBContext.newInstance(Item.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);
marshaller.marshal(item, response.getOutputStream());
}
}catch (Exception e) {
throw new ServletException(e);
}}
如果您想更新一个商品,您需要调用如下的REST风格的URI,其中包含了商品的Id信息。
PUT http://localhost/SoaBookREST/itemService/14
其实,REST的这种调用哲学和HTTP请求的自解释性的特点是一致的,上面的URI可以读作“更新14号商品”。
一般说来,如果您要使用上面的方法创建非CRUD服务,您有以下两种选择:
(1) 使用合适的谓词,新建一个Servelet
(2) 重用已有的Servlet和谓词,但需要添加新的代码,以解析和组装HTTP请求中的业务逻辑。
您也可以考虑在请求中加入参数这种比较实用的办法,请求中添加的参数可以让执行的业务流程发生变化,虽然REST的追随者不喜欢这样做。这种方法的请求格式可能如下所示:
http://localhost/SoaBookREST/itemService?id=14
REST追随者们对上面的办法可能颇有微词,实际上,上面的这种请求格式依赖于具体的参数名(id),这多少为REST这种简单线性的请求风格增添了复杂性。
本文介绍了RESTful API设计中常见的操作如查询、删除等,并通过具体示例展示了如何使用HTTP谓词与URI来清晰地表达服务请求。同时,还探讨了在REST架构下创建非CRUD服务的不同方式。
240

被折叠的 条评论
为什么被折叠?



