Java REST 可以在 POST 后返回内容吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1829875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it ok by REST to return content after POST?
提问by del-boy
I am using RESTlet and I have created a resource. I handle POST by overriding acceptRepresentation
method.
我正在使用 RESTlet 并创建了一个资源。我通过覆盖acceptRepresentation
方法处理 POST 。
The client should send me some data, then I store it to DB, set response to 201 (SUCCESS_CREATED) and I need to return some data to the client, but return type of acceptRepresentation
is void
.
客户端应该向我发送一些数据,然后我将其存储到数据库中,将响应设置为 201 (SUCCESS_CREATED) 并且我需要向客户端返回一些数据,但返回类型acceptRepresentation
是void
.
In my case, I need to return some identificator so that client can access that resource.
就我而言,我需要返回一些标识符,以便客户端可以访问该资源。
For example, if I had a resource with URL /resource
and the client sends POST request I add a new row in DB and its address should be /resource/{id}
. I need to send {id}
.
例如,如果我有一个带有 URL 的资源/resource
并且客户端发送 POST 请求,我会在 DB 中添加一个新行,它的地址应该是/resource/{id}
. 我需要发送{id}
。
Am I doing something wrong? Does REST principles allow to return something after POST? If yes, how can I do it, and if no what is the way to handle this situation?
难道我做错了什么?REST 原则是否允许在 POST 后返回某些内容?如果是,我该怎么做,如果不是,处理这种情况的方法是什么?
采纳答案by Darrel Miller
REST just says that you should conform to the uniform interface. In other words, it says you should do what POST is supposed to do as per the HTTP spec. Here is the quote from that spec that is relevant,
REST 只是说你应该遵守统一接口。换句话说,它说你应该按照HTTP 规范做 POST 应该做的事情。这是该规范的相关引用,
If a resource has been created on the origin server, the response SHOULD be 201 (Created) and contain an entity which describes the status of the request and refers to the new resource, and a Location header (see section 14.30).
如果在源服务器上创建了资源,则响应应该是 201(已创建)并包含一个实体,该实体描述请求的状态并引用新资源,以及一个位置标头(参见第 14.30 节)。
As you can see from this, you have two places where you can indicate to the client where the newly created resource resides. The Location header should have an URL that points to the new resource and you can return an entity with the details also.
从中可以看出,您有两个地方可以向客户端指示新创建的资源所在的位置。Location 标头应该有一个指向新资源的 URL,您也可以返回一个包含详细信息的实体。
I'm not sure what the difference between overriding acceptRepresentation() and overriding post() but thisexample shows how to return a response from a POST.
我不确定覆盖 acceptRepresentation() 和覆盖 post() 之间有什么区别,但此示例显示了如何从 POST 返回响应。
回答by Samir Talwar
Output it in whatever format is requested. That might be:
以任何要求的格式输出它。那可能是:
<success>
<id>5483</id>
</success>
Or:
或者:
{ "type": "success", "id": 5483 }
It depends on what you usually do. If they're not expecting the data, they should just ignore it, but any client that wants to handle it properly should be able to.
这取决于你通常做什么。如果他们不期待数据,他们应该忽略它,但是任何想要正确处理它的客户端都应该能够。
回答by Thom
Two different questions:
两个不同的问题:
Does the REST application pattern support returning data in a POST?
REST 应用程序模式是否支持在 POST 中返回数据?
I don't think REST explicitly disallows it, but the preferred treatment is spelled out in Darrel's answer.
我不认为 REST 明确禁止它,但在 Darrel 的回答中详细说明了首选的处理方式。
Does the RESTlet framework allow returning data in a POST?
RESTlet 框架是否允许在 POST 中返回数据?
Yes, even though it returns void, in a class which extends Resource, you have full access to the Response object object via the getResponse() method. So you can call getResponse().setEntity() with whatever data you want.
是的,即使它返回 void,在扩展 Resource 的类中,您也可以通过 getResponse() 方法完全访问 Response 对象对象。所以你可以用你想要的任何数据调用 getResponse().setEntity() 。
回答by Mike
If you respond 201 Created with an entity body, rather than a Location redirect, then it's a good idea to include a Content-Location header pointing to the resource that is being represented in the response.
如果您使用实体正文而不是位置重定向响应 201 Created,那么最好包含一个 Content-Location 标头,指向响应中表示的资源。
This will avoid potential confusion - in which a client could (justifiably) assume that the response entity actually represents a new state of the 'creator', and not the created resource.
这将避免潜在的混淆——客户端可以(合理地)假设响应实体实际上代表了“创建者”的新状态,而不是创建的资源。
> POST /collection
> ..new item..
< 201 Created
< Location: /collection/1354
< Content-Location: /collection/1354
< <div class="item">This is the new item that was created</div>
回答by cdent
I'd forgo sending anything in the body of the response. Just set Location: to the (full) URL of the newly created resource.
我会放弃在响应正文中发送任何内容。只需将 Location: 设置为新创建资源的(完整)URL。
Your description suggests that this is exactly the semantics you:
您的描述表明这正是您的语义:
- POST a thing to create it
- Respond with enough to know two things:
- That the creation happened (the 201)
- Where to find the new thing (the Location header)
- 发布一个东西来创建它
- 回答足够了解两件事:
- 创造发生了(201)
- 在哪里可以找到新事物(位置标题)
Anything else is superfluous.
其他任何东西都是多余的。