Spring Annotation: replacing @ControllerAdvice



我的任务是删除位于旧应用程序中的@ControllerAdvice(basePackages={quot;com.bla.controller"}(提供的功能。这个功能变得过时了,产生了一些意想不到的错误处理问题;全局";使用@ControllerAdvice的功能由一个单独的团队安装。而这";全局";功能涵盖了大多数需求,旧的应用程序仍然需要自定义错误处理,但没有@ControllerAdvice。我对Spring Annotations的了解非常有限,我希望有人能很好地知道可以使用什么,这样旧的应用程序就能知道在哪里发送错误以进行错误处理。以下是文件中使用@ControllerAdvice(basePackages={quot;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());
}

到目前为止,如果不使用@ControllerAdvice,我无法找到将所有@ExceptionHandler方法移动到一个地方的方法,但我确实发现这个页面总结了Spring的错误处理,我发现它在理解Spring的错误处理方面很有用:使用Spring Boot';s ErrorController和Spring';s ResponseEntityExceptionHandler正确

相关内容

  • 没有找到相关文章

最新更新