使用 REST (restlet) 对结果实现 getNext 的最佳方法是什么
我正在编写一个返回结果的网络服务。此结果是一直在服务器上创建的一些数据。因此,当客户端请求资源时,我想给他一个用于下一个查询的 URL,他可以执行以获取在服务器上创建的新数据。 例如,客户端可以从以下 url: 开始
http://ip:port/server/{id}/resource
,下次他应该使用类似以下内容:
http://ip:port/server/{id}/resource/1234
其中 1234 是指向服务器的一些指针,以了解客户端已经收到哪个结果。 那么问题来了,我在哪里返回下一组结果的url呢?它应该在标题中还是在正文中? 我读了一些关于 url 参数与查询的使用的参考资料,如果我理解 chaching,我最好使用 uri 而不是查询。 最后一件事,我需要传递请求正文的信息,因此 Web 服务需要 PUT 而不是 GET。 Restlet 示例将受到高度赞赏。
最后一件事,我必须在 url 中包含 {id},但是没有这样的 uri
http://ip:port/server
,那么用户知道 id 的正确方法是什么? (结果按用户 ID 返回)。由完全不同的资源分配的 id 。
I'm writing a web service which returns results. This results are some data that is being created on the server all the time. So when the client asks for a resource i want to give him also a url for the next query he can preform to get the new data created on the server.
For example, client can start with the following url:
http://ip:port/server/{id}/resource
and next time he should use something like:
http://ip:port/server/{id}/resource/1234
where 1234 is some pointer to the server to know which result the client has already recieved.
So the question is, where do i return the url to the next set of results? should it be in the header or in the body?
I read some reference about usage of url parameters vs. query, and if i understood for chaching i better use uri rather then query.
Last thing, i need to pass info the body for the request and therefore the web service expects PUT and not GET.
Restlet example will be mostly appreciated.
One last thing, i must have {id} in the url, but there is no such uri as
http://ip:port/server
so what would be the right way for users to know there id? (the results are returned per user's id). the id ia allocated by completly different resource.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
您可以考虑使用条件 GET 的替代方法。在此方法中,服务器使用 ETag 和/或 Last-Modified 响应标头响应 GET 请求,以指示资源上次修改的时间。在上面的示例中,这将是“1234”。然后,客户端在后续 GET 上将该值提供给与之前相同的 URI,并在 If-None-Match 或 If-Modified-Since 标头中提供该值。
我有一个使用 Restlet 实现的类似服务,它不断接收更新,同样,客户端希望定期获取比之前收到的更新的信息。我使用 ETag。
在我的 ServerResource 派生类中,我有这样的代码:
这里的一个技巧是您不能直接在 getFoo() 方法中设置 ETag 标头值;您必须将其隐藏在字段中并在 toRepresentation() 重写中使用它。这是因为在 Restlet 中创建响应表示的时间安排。
You might consider an alternate approach using conditional GETs. In this approach, the server responds to GET requests with an ETag and/or Last-Modified response header to indicate when the resource was last modified. This would be "1234" in your example above. The client then supplies that value on a subsequent GET to the same URI as earlier, providing it in an If-None-Match or If-Modified-Since header.
I have a similar service implemented using Restlet that is continuously receiving updates, and likewise, clients want to periodically fetch information newer than what they received previously. I use ETags.
In my ServerResource-derived class, I have code like this:
One trick here is that you can't set the ETag header value directly in the getFoo() method; you have to tuck it away in a field and use it in the toRepresentation() override. This is because of the timing of when the response Representation is created in Restlet.