ControllerAdvice中@ExceptionHandler注解方法抛出异常时未触发
问题:@ExceptionHandler标注的方法未被调用
我遇到一个问题:标注了@ExceptionHandler的方法在抛出异常时并未被调用。我尝试过在@ExceptionHandler(...)注解中传入具体异常及其父类作为参数,也进行了调试,在标注@ExceptionHandler的handleExceptions()方法内设置了断点,但程序并未进入该方法。
我曾怀疑Spring上下文未将我的GlobalExceptionHandler类识别为Spring Bean,于是查看了调试日志,发现该类已被注册为单例Bean:
2024-03-06 11:54:29.123 DEBUG 10196 --- [main] o.s.b.f.s.DefaultListableBeanFactory: Creating shared instance of singleton bean 'globalExceptionHandler'
我完全不清楚问题出在哪里,提前感谢您的帮助。
项目结构
pl/ ├─ esp/ │ ├─ document/ │ │ ├─ controller/ │ │ │ └─ DocumentController.java │ │ ├─ exception/ │ │ │ ├─ GlobalExceptionHandler.java │ │ │ ├─ PublishDocumentException.java │ │ │ ├─ HttpRuntimeException.java │ │ │ └─ GeneralRuntimeException.java │ │ ├─ service/ │ │ │ ├─ DocumentService.java │ │ │ └─ DocumentServiceImpl.java │ │ ├─ dto/ │ │ │ ├─ ErrorResponse.java │ │ │ └─ PublishDocumentResponse.java │ └─ DocumentServiceApplication.java └─ o4b/
主类
@EnableFeignClients @EnableDiscoveryClient @SpringBootApplication(scanBasePackages = {"pl.esp", "pl.o4b"}) public class DocumentServiceApplication { public static void main(String[] args) { SpringApplication.run(DocumentServiceApplication.class, args); } }
全局异常处理器类
@RestControllerAdvice @Slf4j //试过加@Component注解,虽然@RestControllerAdvice本身已经包含该注解,但没用 public class GlobalExceptionHandler { @ExceptionHandler(PublishDocumentException.class) public ResponseEntity<ErrorResponse> handleExceptions(final PublishDocumentException ex) { log.info("Exception thrown {}", ex.getClass().getName()); final ErrorResponse errorResponse = new ErrorResponse(); errorResponse.setDescription(String.valueOf(ex.getCause())); errorResponse.setStatusCode(HttpStatus.valueOf(ex.code())); errorResponse.setMessage(ex.getMessage()); return new ResponseEntity<>(errorResponse, errorResponse.getStatusCode()); } }
ErrorResponse类
@Getter @Setter //也试过加@Component注解 public class ErrorResponse { private String description; private HttpStatus statusCode; private String message; private String reference; }
抛出异常的业务实现类
@ApplicationService @AllArgsConstructor(access = AccessLevel.PACKAGE) @Slf4j public class DocumentServiceImpl{ public PublishDocumentResponse publishDocument(final String sessionId) { //...业务逻辑... throw new PublishDocumentException(FAILED_TO_GENERATE_PDF_DOCUMENT); } }
PublishDocumentException类
public class PublishDocumentException extends HttpRuntimeException { public PublishDocumentException(final String body) { super(HttpStatus.SC_BAD_REQUEST, body); } }
HttpRuntimeException类
public class HttpRuntimeException extends GeneralRuntimeException implements MessageException<Integer, String, String> { public HttpRuntimeException(int httpStatus, String body) { super(httpStatus, body); } public HttpRuntimeException(int httpStatus, String body, Object... params) { super(httpStatus, body, params); } }
控制器方法
@RestController @AllArgsConstructor(access = AccessLevel.PACKAGE) @Slf4j public class DocumentController { private final DocumentService documentServiceImpl; @GetMapping(value = "/documents/declarations/generate", produces = APPLICATION_JSON_VALUE) public ResponseEntity<PublishDocumentResponse> generateDocument(@RequestParam final String sessionId) { var response = documentServiceImpl.generateDocument(sessionId); log.info("Document generated successfully! Responding status: {}!", HttpStatus.OK); return ResponseEntity.ok(response); } }
内容的提问来源于stack exchange,提问作者Ign
相关产品推荐
相关产品推荐

