【发布时间】:2026-02-03 11:05:01
【问题描述】:
作为 Spring MVC 应用程序的一个好习惯,Web 配置应该只选择“前端”组件,例如 @Controller 或 @RestController。
每个其他 bean 都应该被 Root 应用程序上下文拾取。
我已将 Web 配置定义如下(请记住,我不需要 @EnableMvc 注释,因为它扩展了 WebMvcConfigurationSupport)
@Configuration
@ComponentScan(
basePackages = { ... },
useDefaultFilters = false,
includeFilters = @Filter({
Controller.class,
ControllerAdvice.class}))
以及Root配置如下。
@Configuration
@ComponentScan(
basePackages = { ... },
excludeFilters = @Filter({
Controller.class,
ControllerAdvice.class}))
我定义了两个@RestControllerAdvice 类,第一个捕获所有通用Exception(s),第二个捕获更具体的ServiceException。
当抛出 ServiceException 时,不会调用特定的顾问,而只会选择通用的顾问。两个配置类中的基本包相同。
我还需要在排除和包含过滤器上指定RestControllerAdvice 吗?还是我错过了什么?
编辑:
@RestControllerAdvice 都没有 basePackeges 或任何特定条件。
而ServiceException 确实找到并注册了。
如果我将异常处理程序移动到工作处理程序而不是调用它。
这就是我让它工作的方式。如果我将 ServiceException 处理程序移动到一个单独的类中,它将不再被调用。
@RestControllerAdvice
public class GlobalRestControllerAdviser extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleBindException(
final BindException ex,
final HttpHeaders headers,
final HttpStatus status,
final WebRequest request) {
return new ResponseEntity<Object>(
buildPresentableError(ex.getAllErrors().get(0)),
HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(ServiceException.class)
protected Response<?> handleServiceException(final ServiceException e) {
...
}
@ExceptionHandler(Exception.class)
protected ResponseEntity<Object> handleGenericException(final Exception ex) {
...
}
}
似乎最通用的ExceptionHandler 覆盖了更具体的ExceptionHandler。
【问题讨论】:
标签: java spring spring-mvc annotations