摘要:

本文对RESTful HTTP的基础原理做了一个概览,探讨了开发者在设计RESTful HTTP应用时所面临的典型问题,展示了如何在实践中应用REST架构风格,描述了常用的URI命名方法,讨论了如何使用统一接口进行资源交互,何时使用PUT或POST以及如何支持非CURD操作等。

Java 调用

  1. HttpClient httpClient = new HttpClient();
  2.  
  3. IHttpRequest request = new GetRequest(centralHotelURI);
  4. IHttpResponse response = httpClient.call(request);
  1. REQUEST:
  2. GET /hotel/656bcee2-28d2-404b-891b HTTP/1.1
  3. Host: localhost
  4. User-Agent: xLightweb/2.6
  5.  
  6. RESPONSE:
  7. HTTP/1.1 200 OK
  8. Server: xLightweb/2.6
  9. Content-Length: 277
  10. Content-Type: application/x-www-form-urlencoded
  11.  
  12. classification=Comfort&name=Central&RoomURI=http%3A%2F%2Flocalhost%2Fhotel%2F
  13. 656bcee2-28d2-404b-891b%2FRoom%2F2&RoomURI=http%3A%2F%2Flocalhost%2Fhotel%2F6
  14. 56bcee2-28d2-404b-891b%2FRoom%2F1

指定get部分属性

  1. REQUEST:
  2. GET /hotel/656bcee2-28d2-404b-891b/classification HTTP/1.1
  3. Host: localhost
  4. User-Agent: xLightweb/2.6
  5. Accept: application/x-www-form-urlencoded
  6.  
  7. RESPONSE:
  8. HTTP/1.1 200 OK
  9. Server: xLightweb/2.6
  10. Content-Length: 26
  11. Content-Type: application/x-www-form-urlencoded; charset=utf-8
  12.  
  13. classification=Comfort
  1. REQUEST:
  2. GET /hotel/656bcee2-28d2-404b-891b/classification,name HTTP/1.1
  3. Host: localhost
  4. User-Agent: xLightweb/2.6
  5. Accept: application/x-www-form-urlencoded
  6.  
  7. RESPONSE:
  8. HTTP/1.1 200 OK
  9. Server: xLightweb/2.6
  10. Content-Length: 43
  11. Content-Type: application/x-www-form-urlencoded; charset=utf-8
  12.  
  13. classification=Comfort&name=Central
  1. REQUEST:
  2. GET /hotel/656bcee2-28d2-404b-891b?reqAttr=classification&reqAttr=name HTTP/1.1
  3. Host: localhost
  4. User-Agent: xLightweb/2.6
  5. Accept: application/x-www-form-urlencoded
  6.  
  7. RESPONSE:
  8. HTTP/1.1 200 OK
  9. Server: xLightweb/2.6
  10. Content-Length: 43
  11. Content-Type: application/x-www-form-urlencoded; charset=utf-8
  12.  
  13. classification=Comfort&name=Central

JSON格式:

  1. REQUEST:
  2. GET /hotel/656bcee2-28d2-404b-891b HTTP/1.1
  3. Host: localhost
  4. User-Agent: xLightweb/2.6
  5. Accept: application/json
  6.  
  7. RESPONSE:
  8. HTTP/1.1 200 OK
  9. Server: xLightweb/2.6
  10. Content-Length: 263
  11. Content-Type: application/json; charset=utf-8
  12.  
  13. {"classification":"Comfort",
  14. "name":"Central",
  15. "RoomURI":["http://localhost/hotel/656bcee2-28d2-404b-891b/Room/1",
  16. "http://localhost/hotel/656bcee2-28d2-404b-891b/Room/2"]}

XML格式:

  1. REQUEST:
  2. GET /hotel/656bcee2-28d2-404b-891b HTTP/1.1
  3. Host: localhost
  4. User-Agent: xLightweb/2.6
  5. Accept: text/xml
  6.  
  7. RESPONSE:
  8. HTTP/1.1 406 No match for accept header
  9. Server: xLightweb/2.6
  10. Content-Length: 1468
  11. Content-Type: text/html; charset=iso-8859-1
  12.  
  13. <html>
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
  16. <title>Error 406 No match for accept header</title>
  17. </head>
  18. <body>
  19. <h2>HTTP ERROR: 406</h2><pre>No match for accept header</pre>
  20.  
  21. ...
  22. </body>
  23. </html>

PUT方法:

  1. REQUEST:
  2. PUT Hotel/guest/bc45-9aa3-3f22d HTTP/1.1
  3. Host: localhost
  4. User-Agent: xLightweb/2.6
  5. Content-Length: 94
  6. Content-Type: application/x-www-form-urlencoded
  7.  
  8. zip=30314&lastName=Gump&street=42+Plantation+Street&firstName=Forest&country=US&
  9. city=Baytown&state=LA
  10.  
  11. RESPONSE:
  12. HTTP/1.1 200 OK
  13. Server: xLightweb/2.6
  14. Content-Length: 36
  15. Content-Type: text/plain; charset=utf-8
  16. Location: http://localhost/guest/bc45-9aa3-3f22d
  17.  
  18. The guest resource has been updated.

POST创建:

  1. REQUEST:
  2. POST /HotelHTTP/1.1
  3. Host: localhost
  4. User-Agent: xLightweb/2.6
  5. Content-Length: 35
  6. Content-Type: application/x-www-form-urlencoded; charset=utf-8
  7. Accept: text/plain
  8.  
  9. classification=Comfort&name=Central
  10.  
  11. RESPONSE:
  12. HTTP/1.1 201 Created
  13. Server: xLightweb/2.6
  14. Content-Length: 40
  15. Content-Type: text/plain; charset=utf-8
  16. Location: http://localhost/hotel/656bcee2-28d2-404b-891b
  17.  
  18. the Hotelresource has been created

RESTful HTTP实践的更多相关文章

  1. Nodejs RESTFul架构实践之api篇(转)

    why token based auth? 此段摘自 http://zhuanlan.zhihu.com/FrontendMagazine/19920223 英文原文 http://code.tuts ...

  2. 前后端分离-Restful最佳实践

    前后端分离-Restful最佳实践 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任.

  3. Spring Boot 2 快速教程:WebFlux Restful CRUD 实践(三)

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 这是泥瓦匠的第102篇原创 03:WebFlux Web CR ...

  4. 【转】最佳Restful API 实践

    原文转自:https://bourgeois.me/rest/ REST APIs are a very common topic nowaday; they are part of almost e ...

  5. RESTful最佳实践之基于 jersey 的增删改查

    jersey-rest-demo 增删改查 项目地址:https://github.com/CoderDream/jersey-rest-demo 源代码:http://download.csdn.n ...

  6. 基于Spring Boot的RESTful API实践(一)

    1. RESTful简述    REST是一种设计风格,是一组约束条件及原则,而遵循REST风格的架构就称为RESTful架构,资源是RESTful的核心,一个好的RESTful架构,通过URL就能很 ...

  7. 【Restful】三分钟彻底了解Restful最佳实践

    REST是英文representational state transfer(表象性状态转变)或者表述性状态转移;Rest是web服务的一种架构风格;使用HTTP,URI,XML,JSON,HTML等 ...

  8. RESTful最佳实践

    哲学 不要为了RESTful而RESTful 在能表达清楚的情况下,简单就是美 接口路径设计 接口设计原则 URI指向的是唯一的资源对象 示例: 指向ID为yanbo.ai的Account对象 GET ...

  9. 通俗易懂的RESTful API实践详解(含代码)

    来源:点击进入 点击上方链接,版面更好 一.什么是RESTful REST 是面向资源的,这个概念非常重要,而资源是通过 URI 进行暴露,URI 的设计只要负责把资源通过合理方式暴露出来就可以了,对 ...

随机推荐

  1. shell获取db信息及上传下载操作

    这个脚本是获取目标机器的db信息和os信息的.os信息很好获取,db的信息包含db名字,db版本以及所有的db instance,db信息的获取稍显复杂,下面是整个脚本代码: 关键字: awk, se ...

  2. PowerShell:Linux程序员喜欢的cmd增强版

    Linux程序员有时偶尔使用Windows下的cmd工具,会被逼疯的,有些命令ls, cat, ps等已经条件反射一样使用. 但在cmd下,根本不知道该用什么命令,好在盖兹大叔照顾了此部分需求.从Vi ...

  3. Linux 下文件名乱码(无效的编码)的解决办法

    文件是在WIndows 下创建的,Windows 的文件名中文编码默认为GBK,而Linux中默认文件名编码为UTF8,由于编码 不一致所以导致了文件名乱码的问题,解决这个问题需要对文件名进行转码. ...

  4. 在C中定义一个动态的二维数组

    一般来讲两种办法: 第一种:连续内存分配 #include "stdio.h" #include "stdlib.h" int main() { int x,y ...

  5. return遇到finally

    public class Test { public static void main(String[] args) { System.out.println("=============t ...

  6. hdu5785--Interesting(manacher)

    题意:求给定字符串的三元组(I,J,K)  使得S[i..j] 和 S[j+1..k] 都是回文串.求所有满足条件的三元组 ∑(i*k) 题解:求出以j为结尾的回文串起始位置的和记为lv[j],和以j ...

  7. JS 格式化日期

    function formatDate(date){ var year=date.getFullYear(); var month=date.getMonth()+1; var date=date.g ...

  8. java properties 文件中书写相对路径

    工程src下的properties 文件要引用发布到D:\work\apache-tomcat-7.0.52\webapps\项目名称\certs这个地址下的文件,properties 中的文件路径应 ...

  9. PING命令入门详解(转载)

    本文转自http://www.linkwan.com/gb/tech/htm/928.htm 1.Ping的基础知识 ping命令相信大家已经再熟悉不过了,但是能把ping的功能发挥到最大的人却并不是 ...

  10. Android 图片加载[常见开源项目汇总]

    该文主要是讲一下目前有哪些使用比较多的 图片加载开源项目,并简单介绍该如果使用 以及各开源项目之间有什么区别, 我们该如何去选择适合的开源项目应用到我们的项目中? 一.Android-Universa ...