设计一个RESTful网站 [英] Designing a RESTful website

查看:132
本文介绍了设计一个RESTful网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习创建RESTful Web应用程序。我有一些疑问。

I am trying to learn to create RESTful web applications. I have a few doubts.


  1. 说,我要求在网站标题上显示用户名。我以前是通过在会话中存储用户对象然后在JSP中获取名称来实现的。但是现在,不是存储会话数据和破坏REST约定吗?只要用户登录(过度杀戮),我是否必须在每个响应中将名称发送给客户?

  2. 我在许多网站中看到了URL,包括模式中的SO 问题/ 4135336 /正确的休息-URI设计。不会 questions / 4135336 足够,假设 4135336 是ID吗?然后是什么呢?另一个身份?生成它的任何标准?

  3. 我已经读过特定资源,比如说/ students / {student} CRUD操作应该使用GET(fetch),POST(更新),PUT来完成(创建/覆盖),DELETE(删除)。如果它是由人阅读的应用程序,我们是否需要这些约定。例如:POST不会通过发送适当的参数进行删除吗?我们想要实现什么目标?

  1. Say, I have a requirement to display the name of user on the website header. I used to do it by storing the user object in session and then fetching the name in the JSP. But now, isn't that storing a conversational data and breaking REST convention? Do I have to send the name to the client in every response as long as the user is logged in (overkill)?
  2. I have seen URLs in many websites including SO in the pattern questions/4135336/correct-rest-uri-design. Wouldn't questions/4135336 suffice, assuming 4135336 is the ID? Then what is that comes after that? another id? any standard to generate it?
  3. I have read that for a particular resource, say /students/{student} CRUD operations should be done using GET (fetch), POST (update), PUT (create/overwrite) ,DELETE (delete). If its an application read by a human, do we need these conventions. For eg: Wouldn't a POST do a delete by sending appropriate params? what are we trying to achieve?

提前致谢:)

推荐答案

REST 背后的全部理念是每个请求都是完整的从服务器的角度独立于任何其他。如果有的话,客户负责维护状态。对于给定的请求,服务器将信息传送到满足请求所需的客户端,并且还可以传送允许客户端通过发出更多请求来查找附加信息的信息。考虑到这一点:

The whole idea behind REST is that each request is completely independent from any other from the server's perspective. The client is responsible for maintaining state, if any. For a given request, the server transfers information to the client needed to satisfy the request, and may also transfer information which allows the client to be able to find additional information by making more requests. With that in mind:


  1. 这取决于您网站的设计,以及被视为资源的内容。如果客户端请求页面,并且该页面被视为单个资源单元,则是,服务器将需要返回用户名作为任何请求的响应的一部分。如果设计是这样的,客户端可以分批询问事物,那么客户端有责任在请求中请求用户名,然后在标题中呈现它。页面其余部分的内容将使用其他单独的请求进行呈现。

  2. ID足够。 URL的其余部分仅用于人类的可读性,因为人类不容易记住415336是标题为正确的休息Uri设计的文章的ID。服务器不使用URL中的这些额外信息来查找项目;只使用ID。因此,它本身并不是REST本身的一部分,它只是网站提供的一种精确性。

  3. 理想情况下,REST应该是客户端无关的;从理论上讲,您可以编写一个通用的REST客户端,它可以导航任何支持REST的服务器,并且该客户端将能够发现服务器上的资源并能够操作它们。这是可能的,因为正如您所指出的,REST利用HTTP谓词的标准词汇来表示常见的CRUD操作。如果您将HTTP谓词重载为其他内容,则通用客户端可能无法导航该站点。此外,如果你重载GET以产生副作用,例如更新或删除信息,那么通用客户端(想象一个网络爬虫)可能最终只是通过试图找出网站上可用的内容来破坏信息。这绝不是一个好主意。

这篇关于设计一个RESTful网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆