【问题标题】:@RestControllerAdvice vs @ControllerAdvice@RestControllerAdvice 与 @ControllerAdvice
【发布时间】:2017-03-30 17:16:20
【问题描述】:
  • @RestControllerAdvice 和@ControllerAdvice 之间的主要区别是什么??
  • 我们是否应该始终将@RestControllerAdvice 用于休息服务和@ControllerAdvice MVC?

【问题讨论】:

    标签: spring


    【解决方案1】:

    @RestControllerAdvice 只是@ControllerAdvice + @ResponseBody 的语法糖,你可以看看here

    我们是否应该始终将@RestControllerAdvice 用于休息服务和 @ControllerAdvice MVC?

    同样,如上所述,@ControllerAdvice 也可用于 REST Web 服务,但您需要额外使用 @ResponseBody

    【讨论】:

    • 你能给我们举个例子吗?
    【解决方案2】:

    另外,我们可以理解为:

    @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;
      }
    }
    

    在这种情况下,任何异常 instanceOf CustomNotFoundException 都会在响应体中抛出。

    此处摘录的示例: https://grokonez.com/spring-framework/spring-mvc/use-restcontrolleradvice-new-features-spring-framework-4-3

    【讨论】:

    • 您的意思是“@RestControllerAdvice”在这个语句中吗:“记住@ControllerAdvice 是更方便的注解,用于使用RestfulApi 处理异常。”
    • @huypham99 是的,你是对的,谢谢你的提示。
    • 链接已损坏。
    • 明天我会试着找一个例子。
    【解决方案3】:

    异常:一个好的 REST API 应该正确处理异常并向用户发送正确的响应。不应以任何未处理的异常呈现用户。 REST API 开发人员将有两个与错误处理相关的要求。

    1. 错误处理的共同点
    2. 类似的错误响应正文,跨 API 具有正确的 HTTP 状态代码

    @RestControllerAdvice 是@ControllerAdvice 和@ResponseBody 的组合

    @ControllerAdvice 注解最早是在 Spring 3.2 中引入的。

    我们可以使用@ControllerAdvice注解来处理RESTful Services中的异常,但是我们需要单独添加@ResponseBody

    注意:
    GlobalExceptionHandler 使用@ControllerAdvice 进行注释,因此它将拦截来自整个应用程序的控制器的异常。

    【讨论】:

      【解决方案4】:

      @RestControllerAdvice 和@ControllerAdvice 的区别在于:

      @RestControllerAdvice = @ControllerAdvice + @ResponseBody。 - 我们可以 在 REST 网络服务中使用。

      @ControllerAdvice - 我们可以在 MVCRest web services 中使用,需要 如果我们在 Rest web 服务中使用它,请提供 ResponseBody。

      例如:

      异常类:

      @ResponseStatus(value = HttpStatus.NOT_FOUND)
      public class ResourceNotFoundException extends Exception{
      
          private static final long serialVersionUID = 1L;
          public ResourceNotFoundException(String message){
              super(message);
          }
      }
      

      在 Rest Web Service 中使用上述异常。

       @RestControllerAdvice
       public class MyRestControllerAdviceHandler {
           
       @ExceptionHandler(ResourceNotFoundException.class)
            public ResponseMsg resourceNotFoundException(ResourceNotFoundException ex) {
              ResponseMsg resMsg = new ResponseMsg(ex.getMessage());
              return resMsg;
            }
          
      }
      

      上述异常在 MVC 中的使用。

      @ControllerAdvice
      public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
          @ExceptionHandler(ResourceNotFoundException.class)
          public ResponseEntity<?> resourceNotFoundException(ResourceNotFoundException ex) {
              return new ResponseEntity<>(ex.getMessage(), HttpStatus.NOT_FOUND);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-06
        • 2019-08-12
        • 1970-01-01
        • 1970-01-01
        • 2021-01-27
        • 2021-10-31
        • 2019-02-22
        • 2023-01-31
        相关资源
        最近更新 更多