• RESTful HTTP实践


    摘要:

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

    Java 调用

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

    指定get部分属性

    REQUEST:
    GET /hotel/656bcee2-28d2-404b-891b/classification HTTP/1.1
    Host: localhost
    User-Agent: xLightweb/2.6
    Accept: application/x-www-form-urlencoded
    
    
    RESPONSE:
    HTTP/1.1 200 OK
    Server: xLightweb/2.6
    Content-Length: 26
    Content-Type: application/x-www-form-urlencoded; charset=utf-8
    
    
    classification=Comfort
    REQUEST:
    GET /hotel/656bcee2-28d2-404b-891b/classification,name HTTP/1.1
    Host: localhost
    User-Agent: xLightweb/2.6
    Accept: application/x-www-form-urlencoded
    
    
    RESPONSE:
    HTTP/1.1 200 OK
    Server: xLightweb/2.6
    Content-Length: 43
    Content-Type: application/x-www-form-urlencoded; charset=utf-8
    
    
    classification=Comfort&name=Central
    REQUEST:
    GET /hotel/656bcee2-28d2-404b-891b?reqAttr=classification&reqAttr=name HTTP/1.1
    Host: localhost
    User-Agent: xLightweb/2.6
    Accept: application/x-www-form-urlencoded
    
    
    RESPONSE:
    HTTP/1.1 200 OK
    Server: xLightweb/2.6
    Content-Length: 43
    Content-Type: application/x-www-form-urlencoded; charset=utf-8
    
    
    classification=Comfort&name=Central

    JSON格式:

    REQUEST:
    GET /hotel/656bcee2-28d2-404b-891b HTTP/1.1
    Host: localhost
    User-Agent: xLightweb/2.6
    Accept: application/json
    
    
    RESPONSE:
    HTTP/1.1 200 OK
    Server: xLightweb/2.6
    Content-Length: 263
    Content-Type: application/json; charset=utf-8
    
    
    {"classification":"Comfort",
    "name":"Central",
    "RoomURI":["http://localhost/hotel/656bcee2-28d2-404b-891b/Room/1",
           "http://localhost/hotel/656bcee2-28d2-404b-891b/Room/2"]}

    XML格式:

    REQUEST:
    GET /hotel/656bcee2-28d2-404b-891b HTTP/1.1
    Host: localhost
    User-Agent: xLightweb/2.6
    Accept: text/xml
    
    
    RESPONSE:
    HTTP/1.1 406 No match for accept header
    Server: xLightweb/2.6
    Content-Length: 1468
    Content-Type: text/html; charset=iso-8859-1
    
    
    <html>
       <head>
          <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
          <title>Error 406 No match for accept header</title>
       </head>
       <body>
           <h2>HTTP ERROR: 406</h2><pre>No match for accept header</pre>
    
    
             ...
       </body>
    </html>

    PUT方法:

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

    POST创建:

    REQUEST:
    POST /HotelHTTP/1.1
    Host: localhost
    User-Agent: xLightweb/2.6
    Content-Length: 35
    Content-Type: application/x-www-form-urlencoded; charset=utf-8
    Accept: text/plain
    
    
    classification=Comfort&name=Central
    
    
    RESPONSE:
    HTTP/1.1 201 Created
    Server: xLightweb/2.6
    Content-Length: 40
    Content-Type: text/plain; charset=utf-8
    Location: http://localhost/hotel/656bcee2-28d2-404b-891b
    
    
    the Hotelresource has been created
  • 相关阅读:
    docker命令总结
    VulToEs
    MYSQL
    MoonStack
    Spring mvc json null
    MySQL
    极光推送
    坑爹的RockSaw和坑爹的windows7
    App接口设计思路
    CSUOJ 1329 一行盒子(数组模拟链表)
  • 原文地址:https://www.cnblogs.com/feika/p/3574672.html
Copyright © 2020-2023  润新知