宁静的背景或实体?

时间:2014-07-14 21:01:03

标签: api rest restful-architecture

在Rest API中提供有关对象的信息的最佳方法是什么?

  1. 所有整个对象总是:

    Request:
    GET /user/{idUser}
    
    Response:
    {
    id,
    name,
    birthday,
    street,
    city,
    state,
    country
    }
    
  2. 在对象中重要的部分,由上下文标识:

    Request:
    GET /user/address/{idUser}
    
    Response:
    {
    id,
    street,
    city,
    state,
    country
    }
    

1 个答案:

答案 0 :(得分:0)

(2)应该避免,因为它会导致端点的扩散。 (1)是可以接受的,但可能会受到性能影响,因为它会推送比通过线路所需的信息更多的信息。如果您测试性能并发现它不可接受,您可以提供以下内容:

GET /users/bob?fields=id,street,city,state,country
{
    id,
    street,
    city,
    state,
    country
}