java REST - 使用 Spring MVC 返回创建的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16127454/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:59:29  来源:igfitidea点击:

REST - Returning Created Object with Spring MVC

javajsonrestspring-mvcstandards

提问by Kevin Rave

I have a REST call that accepts a JSON object, lets say, a person. After I create this object (validated and saved to the database), I need to return the newly created JSON Object.

我有一个 REST 调用,它接受一个 JSON 对象,比如说,一个人。创建此对象(验证并保存到数据库)后,我需要返回新创建的 JSON 对象。

I think the standard practice is to return 201 Acceptedinstead of returning the object immediately. But my application needs the newly created object immediately.

我认为标准做法是返回201 Accepted而不是立即返回对象。但是我的应用程序立即需要新创建的对象。

I have a controller methods that takes a POST call, calls a service class, which in turn calls a DAO that uses Hibernate to create the object. Once it's saved to the database, I am calling another controller method that takes the ID of the person and returns the Object.

我有一个控制器方法,它接受一个 POST 调用,调用一个服务类,后者又调用一个使用 Hibernate 创建对象的 DAO。将其保存到数据库后,我将调用另一个控制器方法,该方法获取人的 ID 并返回对象。

My question, is this the better approach? That is calling another Controller method to get the newly created object. Or the POST call itself should return the Object.

我的问题是,这是更好的方法吗?那就是调用另一个 Controller 方法来获取新创建的对象。或者 POST 调用本身应该返回对象。

The main question is:Calling another method takes a round trip and I guess it's an overkill. (Service->DAO->Hibernate->Database). Instead I think I should get the Object from the database immediately after it's saved in the same call (from the method that handled POST).

主要问题是:调用另一种方法需要往返一次,我想这是一种矫枉过正。(服务->DAO->休眠->数据库)。相反,我认为我应该在将对象保存在同一个调用中后立即从数据库中获取对象(从处理 POST 的方法)。

What is the architecture standard here?

这里的架构标准是什么?

回答by Jeevan Patil

Try using ResponseEntitywhich returns HTTP status along with the object you need.

尝试使用ResponseEntitywhich 返回 HTTP 状态以及您需要的对象。

Sample code is (this was my code where I am returning Customer object, change it as per your needs) :

示例代码是(这是我返回 Customer 对象的代码,请根据您的需要进行更改):

// imports (for your reference)
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

// spring controller method
@RequestMapping(value = "getcust/{custid}", method = RequestMethod.GET, produces={"application/json"})
public ResponseEntity<Customer> getToken(@PathVariable("custid") final String custid, HttpServletRequest request) {

    customer = service.getCustById(custid);

    return new ResponseEntity<Customer>(customer, HttpStatus.OK);
}

Read this documentationto know more. Some sample code has been provided there.

阅读此文档以了解更多信息。那里提供了一些示例代码。

回答by Perception

From the HTTP specification for POST:

来自POSTHTTP 规范

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 节)。

What you will return in the response body will depend on how strictly you interpret an entity which describes the status of the request and refers to the new resource- and many implementations simply return a representation of the newly created entity itself. The most important thing is to set the Locationheader in the response to be the URI of the newly created resource, so that clients may immediately fetch it if they so choose.

您将在响应正文中返回的内容取决于您解释的严格程度an entity which describes the status of the request and refers to the new resource- 许多实现只是返回新创建的实体本身的表示。最重要的是Location将响应中的标头设置为新创建资源的 URI,以便客户端可以在选择时立即获取它。

回答by Bassem Reda Zohdy

You can return the entity object just after persisting it using @ResponseBody, after @ResponseStatus, but it is not standard, so your client have to be aware about this customization, otherwise if your client depends on standard APIs you have to stuck to the standard by returning void.

您可以在使用@ResponseBody 持久化实体对象后返回实体对象,在@ResponseStatus 之后,但它不是标准的,因此您的客户必须了解此自定义,否则如果您的客户依赖于标准 API,您必须坚持标准通过返回无效。