Given this service to get information about a hotel:
> GET /hotel/{id}
< 开发者_StackOverflow社区HTTP/1.1 200 OK
< <hotel>
< <a>aaa</a>
< <b>aaa</b>
> <biggie>aaa....I am 300K</biggie >
< </hotel>
Problem is that biggie
is 300K and we don't want to return it with every response. What's the RESTful way to lazy load this value?
Should we set up two resources:
> GET /hotel/{id}
< HTTP/1.1 200 OK
< <hotel>
< <a>aaa</a>
< <b>aaa</b>
< </hotel>
and..
> GET /hotel/{id}/biggie
< HTTP/1.1 200 OK
< <biggie>
< <val>aaa....I am 300K</val>
< </biggie>
And you only request GET /hotel/{id}/biggie
when you really need that data?
This would work.. although there is nothing special about biggie
except that it's a large data set. I think it's nicer to keep everything at the hotel
level as all the attributes are really just attributes of the hotel
.
Don't forget, hypermedia is your friend.
GET /hotel/{id}
HTTP/1.1 200 OK
<hotel Id="99">
<a>aaa</a>
<b>aaa</b>
<biggieLink href="/Hotel/99/Biggie"/>
</hotel>
or you can even do
GET /hotel/{id}
HTTP/1.1 200 OK
<hotel Id="99">
<a>aaa</a>
<b>aaa</b>
<biggieSynopsis href="/Hotel/99/Biggie">
<title>Here is a a summary of biggie</title>
</biggieSynopsis
</hotel>
Setting it up as two resources would work, but if you don't like that, you could look into using caching; depending on the nature of the data, that might actually save you more load than splitting into multiple resources.
I think your solution is fine. There's no perfect answer, but three possibilities are:
- Send all attributes, every time
- Send attributes individually as requested (
/{id}/a
,/{id}/b
,/{id}/biggie
) - Send all attributes EXCEPT for unusual ones, as you suggest.
1 and 2 are nice because they're uniform, but 3 makes sense in your case, or in a case where one piece of data requires login credentials, for example.
(2 also has the drawback of requiring tighter coupling with the requester, who has to know the name of every attribute.)
精彩评论