如何配置Spring Data REST以返回为POST请求创建的资源的表示形式? [英] How to configure Spring Data REST to return the representation of the resource created for a POST request?
问题描述
我正在遵循spring-data-rest指南使用REST访问JPA数据一个>.当我http发布新记录时,它将被插入(并且响应为201).很好,但是有没有办法配置REST MVC代码以返回新创建的对象?我宁愿不必发送搜索请求来查找新实例.
I am following the spring-data-rest guide Accessing JPA Data with REST. When I http post a new record it is inserted (and the response is a 201). That is great, but is there a way to configure the REST MVC code to return the newly created object? I'd rather not have to send a search request to find the new instance.
推荐答案
您不必搜索创建的实体.正如HTTP规范所建议的那样,返回状态代码为201 Created
的POST
请求应该包含一个Location
标头,该标头包含刚刚创建的资源的URI.
You don't have to search for the created entity. As the HTTP spec suggests, POST
requests returning a status code of 201 Created
are supposed to contain a Location
header which contains the URI of the resource just created.
因此,您需要做的就是有效地向该特定URI发出GET
请求. Spring Data REST在RepositoryRestConfiguration.setReturnBodyOnCreate(…)
和….setReturnBodyOnUpdate(…)
上也有两个方法,可用于配置框架以立即返回刚刚创建的资源的表示形式.
Thus all you need to do is effectively issuing a GET
request to that particular URI. Spring Data REST also has two methods on RepositoryRestConfiguration.setReturnBodyOnCreate(…)
and ….setReturnBodyOnUpdate(…)
which you can use to configure the framework to immediately return the representation of the resource just created.
这篇关于如何配置Spring Data REST以返回为POST请求创建的资源的表示形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!