英文:
What to return to from a REST API when updates fail
问题
我正在使用Spring Boot编写一个Web应用程序,该应用程序经常更新后端数据并将更新后的对象返回以反映前端的更新。
我遇到的问题是,如果由于某种原因更新失败,应该从我的方法返回什么。
目前,如果更新失败,我会返回接收到的对象,但如今在前端的状态不会反映后端的失败情况。
我想返回对象以更新状态,但这样做会阻止我返回指示问题的String
或HttpStatus
,是吗?返回旧对象似乎也不是一个好的解决方案。
英文:
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状态代码的见解:
-
You can map exceptions to responses by annotating an exception class with
@ResponseStatus
. -
您可以通过使用
@ResponseStatus
注解异常类来将异常映射到响应。 -
It also gives you the possibility to implement a
HandlerExceptionResolver
or extend one of the existing implementations, such as theAbstractHandlerExceptionResolver
. -
它还为您提供了实现
HandlerExceptionResolver
或扩展现有实现(如AbstractHandlerExceptionResolver
)的可能性。 -
Another approach would be using a
ResponseEntityExceptionHandler
annotated with@ControllerAdvice
and define the handled exceptions by annotating the implemented method with@ExceptionHandler
. -
另一种方法是使用带有
@ControllerAdvice
注解的ResponseEntityExceptionHandler
,并通过使用@ExceptionHandler
注解实现的方法来定义已处理的异常。
英文:
> 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:
-
You can map exceptions to responses by annotating an exception class with
@ResponseStatus
. -
It also gives you the possibility to implement a
HandlerExceptionResolver
or extend one of the existing implementations, such as theAbstractHandlerExceptionResolver
. -
Another approach would be using a
ResponseEntityExceptionHandler
annotated with@ControllerAdvice
and define the handled exceptions by annotating the implemented method with@ExceptionHandler
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论