【问题标题】:Spring Annotation: replacing @ControllerAdviceSpring注解:替换@ControllerAdvice
【发布时间】:2022-01-10 19:33:09
【问题描述】:

我的任务是删除旧应用程序中 @ControllerAdvice(basePackages = {"com.bla.controller"}) 提供的功能。在另一个使用@ControllerAdvice 的“全局”功能被一个单独的团队安装之后,这个功能变得过时了,产生了一些意想不到的错误处理问题。 虽然这种“全局”功能满足了大多数需求,但旧应用程序仍然需要自定义错误处理,但没有 @ControllerAdvice。 我对 Spring Annotations 的了解非常有限,我希望有人可能知道可以使用什么来代替,以便旧应用程序知道在哪里发送错误以进行错误处理。 下面是一个使用 @ControllerAdvice(basePackages = {"com.bla.controller"}) 表示法的文件中的实现示例,显示了旧应用程序中的自定义和基本用法:

    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(ResourcePageException.class)
    public String notFound(Model model, ResourcePageException exception) {
        setAttributes(model);
        populateModel(model, exception);
        return Mapping.NO_RESULTS_VIEW;
    }
    
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoCustomSearchResultsRestException.class)
    public ResponseEntity<Object> locationLookupRestError(NoCustomSearchResultsRestException exception) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(exception.getModel());
    }

【问题讨论】:

    标签: spring-annotations


    【解决方案1】:

    到目前为止,在不使用 @ControllerAdvice 的情况下,我无法找到将所有 @ExceptionHandler 方法移动到一个位置的方法,但我确实发现这个页面总结了 Spring 的错误处理,我发现它在以下方面很有用了解 Spring 的错误处理:Using Spring Boot's ErrorController and Spring's ResponseEntityExceptionHandler correctly

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 2021-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      相关资源
      最近更新 更多