从REST API更新失败时返回什么?

huangapple go评论62阅读模式
英文:

What to return to from a REST API when updates fail

问题

我正在使用Spring Boot编写一个Web应用程序,该应用程序经常更新后端数据并将更新后的对象返回以反映前端的更新。

我遇到的问题是,如果由于某种原因更新失败,应该从我的方法返回什么。

目前,如果更新失败,我会返回接收到的对象,但如今在前端的状态不会反映后端的失败情况。

我想返回对象以更新状态,但这样做会阻止我返回指示问题的StringHttpStatus,是吗?返回旧对象似乎也不是一个好的解决方案。

英文:

I am writing a web application using Spring Boot that frequently updates data on the back end and returns the updated object to reflect the update on the front end.

The question I have is what to return from my methods if the update should fail for some reason.

I am currently returning the object as it was received should it fail but as it stands the state on the front end would not reflect the failure on the back end in the case that it occurs.

I want to return the object to update the state but doing so prevents me from returning a String or HttpStatus indicating a problem doesn't it? Returning the old object doesn't seem a good solution either.

答案1

得分: 2

在这种失败情况下,您可以从您的REST控制器中引发异常。

要处理此异常,Spring提供了ResponseEntityExceptionHandler回调类,借助它您可以处理引发的异常并在响应实体中设置不同的标头。

因此,在客户端,您可以识别出服务器端发生了某些故障。

您可以将HttpStatus设置为HttpStatus.INTERNAL_SERVER_ERROR,并在正文中添加更多详细信息。

英文:

You can throw an exception in this case of failure from your REST controller.

To handle this exception, Spring provides ResponseEntityExceptionHandler callback class with the help of which you can handle the thrown exception and set different headers in the response entity.

So on client-side, you can recognise that some failure is occurred on server side.

You can set HttpStatus as HttpStatus.INTERNAL_SERVER_ERROR and add more details in the body.

答案2

得分: 1

以下是翻译好的部分:

The question I have is what to return from my methods if the update should fail for some reason.
我要提出的问题是如果由于某些原因更新失败,从我的方法中应该返回什么。

You first need to determine whether the error was caused by the client or by the server, then you can determine the most suitable status code to be returned, either in the 4xx or in the 5xx range. See this answer which may give you some insights.
首先,您需要确定错误是由客户端引起的还是由服务器引起的,然后您可以确定应返回哪个最合适的状态代码,可以是在4xx范围内,也可以是在5xx范围内。可以参考这个答案,它可能会给您一些见解。

Instead of returning the request request back in the response, you should return a payload that describes what the problem was. Consider, for example, the payload defined in the RFC 7807 along with the application/problem+json media type.
与其在响应中返回请求请求,您应该返回一个描述问题的有效负载。例如,考虑在RFC 7807中定义的有效负载以及application/problem+json媒体类型。

Finally, this answer may give you insights on how to map an exception to a HTTP status code in Spring:
最后,这个答案可能会为您提供有关如何在Spring中将异常映射到HTTP状态代码的见解:

英文:

> The question I have is what to return from my methods if the update should fail for some reason.

You first need to determine whether the error was caused by the client or by the server, then you can determine the most suitable status code to be returned, either in the 4xx or in the 5xx range. See this answer which may give you some insights.

Instead of returning the request request back in the response, you should return a payload that describes what the problem was. Consider, for example, the payload defined in the RFC 7807 along with the application/problem+json media type.

Finally, this answer may give you insights on how to map an exception to a HTTP status code in Spring:

huangapple
  • 本文由 发表于 2020年7月22日 13:23:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63027453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定