Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring @RestControllerAdvice vs@ControllerAdvice @RestControllerAdvice和@ControllerAdvice之间的主要区别是什么 我们是否应该始终为rest服务使用@RestControllerAdvice和@ControllerAdvice MVC @RestControllerAdvice只是@ControllerAdvice+@ResponseBody的一个语法糖,您可以查看_Spring - Fatal编程技术网

Spring @RestControllerAdvice vs@ControllerAdvice @RestControllerAdvice和@ControllerAdvice之间的主要区别是什么 我们是否应该始终为rest服务使用@RestControllerAdvice和@ControllerAdvice MVC @RestControllerAdvice只是@ControllerAdvice+@ResponseBody的一个语法糖,您可以查看

Spring @RestControllerAdvice vs@ControllerAdvice @RestControllerAdvice和@ControllerAdvice之间的主要区别是什么 我们是否应该始终为rest服务使用@RestControllerAdvice和@ControllerAdvice MVC @RestControllerAdvice只是@ControllerAdvice+@ResponseBody的一个语法糖,您可以查看,spring,Spring,我们是否应该始终使用@RestControllerAdvice来提供rest服务和服务 @ControllerAdvice MVC 同样,如上所述,@ControllerAdvice甚至可以用于REST web服务,但您还需要另外使用@ResponseBody此外,我们可以将其理解为: @RestControler=@Controller+@ResponseBody @RestControllerAdvice=@ControllerAdvice+@ResponseBody 请记住,@RestCo

我们是否应该始终使用@RestControllerAdvice来提供rest服务和服务 @ControllerAdvice MVC


同样,如上所述,
@ControllerAdvice
甚至可以用于REST web服务,但您还需要另外使用
@ResponseBody

此外,我们可以将其理解为:

@RestControler
=
@Controller
+
@ResponseBody


@RestControllerAdvice
=
@ControllerAdvice
+
@ResponseBody

请记住,
@RestControllerAdvice
是使用RestfulApi处理异常的更方便的注释

操作系统使用示例:

@RestControllerAdvice
public class WebRestControllerAdvice {
  
  @ExceptionHandler(CustomNotFoundException.class)
  public ResponseMsg handleNotFoundException(CustomNotFoundException ex) {
    ResponseMsg responseMsg = new ResponseMsg(ex.getMessage());
    return responseMsg;
  }
}
在这种情况下,CustomNotFoundException的任何异常实例都将在响应体中抛出

此处提取的示例: 异常:一个好的RESTAPI应该正确处理异常,并向用户发送正确的响应。用户不应呈现任何未处理的异常。 RESTAPI开发人员将有两个与错误处理相关的需求

  • 错误处理的常见位置
  • 类似的错误响应主体,具有跨API的正确HTTP状态代码
  • @RestControllerAdvice是@ControllerAdvice和@ResponseBy的组合

    @ControllerAdvice注释是在Spring 3.2中首次引入的

    我们可以使用@ControllerAdvice注释来处理RESTful服务中的异常,但我们需要单独添加@ResponseBy

    注:

    GlobalExceptionHandler是用@ControllerAdvice注释的,因此它将截获应用程序中来自控制器的异常。

    您能给我们举个例子吗?您在这句话中的意思是“@RestControllerAdvice”:“请记住,@ControllerAdvice是用RestfulApi处理异常更方便的注释。”@是的,你是对的,谢谢你的提示。