java - 初学者对REST服务中POST方法的困惑

标签 java web-services rest jersey

我参加了一场关于Jersey REST Web 服务的公开讲座并做了一些笔记。这是讲师在讲座中说的,我对此感到很困惑。

i.you will need POST some content to /files/. For example, you have a file locally called data.json.Posting the content of that file to your REST service will store the content on the server with a name it chooses automatically, say 3.json, in a folder chosen by you.

ii.The key thing here is the REST service will need to manage the files it creates by choosing a name that hasn't been used and return that name to sender, so the sender can re-download the content it sent using the name it gets.( The sender do not know which name the content will be given until it receives the response from the REST service).

对于步骤 i,这是否意味着将文件上传到服务并保存到新位置?服务将自动为其命名是什么意思?

对于第二步,这意味着如果我将三个文件发送到服务中,当我调用 GET/files/1、GET/files/2 和 GET/files/3 时,将返回 1.json、2 的内容。分别是 json、3.json?另外讲师说我们可以使用curl命令行将文件发布到服务。

讲师没有提供任何例子,让我没有理解清楚。

是否可以帮助我编写一个有关该问题的演示或为我提供一些示例?

最佳答案

you have a file locally called data.json

好吧,很简单

Posting the content of that file to your REST service will store the content on the server

当然,正在保存文件。它读取 POST 数据并将其存储到磁盘。

a name it chooses automatically

这是一个小细节...它可以存储为相同的名称,但这样就会产生冲突的文件名。

in a folder chosen by you

这一点还不太清楚......但是,继续。

the REST service will need to manage the files it creates by choosing a name that hasn't been used

正是之前的观点。

and return that [...] to sender

考虑一个典型的网站...您请求http://stackoverflow.com。它返回 HTML。您的 REST 服务只是返回一个字符串/文件。

return that name to sender, so the sender can re-download the content it sent using the name it gets.( The sender do not know which name the content will be given until it receives the response from the REST service).

客户端需要知道添加的文件名称。否则,你不知道如何请求文件;服务器已为您最初发送给它的文件生成了自己的名称。

<小时/>

if I send three files into the service, when I call GET /files/1, GET /files/2 and GET /files/3 will return the content of 1.json, 2.json, 3.json respectively?

不完全是 - 服务器生成文件的名称。所以,一个更好的例子是

POST -d file.json /files

输出文本

20160801-21-38.json

为了请求取回该文件,您现在需要使用该值

GET /files/20160801-21-38.json

关于java - 初学者对REST服务中POST方法的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38710756/

相关文章:

java - 将 PoJo 的 JSON String 属性解析为对象

java - 最小和最大数字

java - BufferedImage 仅显示为黑色

java - 通过正则表达式从行中获取彼此相邻的 3 个字符串

html - 通过浏览器向正在监听端口的程序发送 HTTP 请求

php - 你调用的对象是空的。 PHP

web-services - 如何使用 JAX-WS 生成 WSDL 2.0

api - 如何为分层实体设计 RESTful API

http - REST API : custom HTTP headers vs URL parameters

java - 如何将 url 作为路径参数传递?在 JAX-RS @Path 中