@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(NoHandlerFoundException.class)
public ResponseEntity<Error> handle(NoHandlerFoundException ex){
String message = "HTTP " + ex.getHttpMethod() + " for " + ex.getRequestURL() + " is not supported.";
Error error = new Error(HttpStatus.NOT_FOUND.value(), message);
return new ResponseEntity<Error>(error, HttpStatus.NOT_FOUND);
}
}
- 我将 @ControllerAdvice 与 @ExceptionHandler 一起使用。
- 我需要获取发生异常的控制器类名称和包名称或句柄方法内的类对象
这应该有效
@ControllerAdvice
class AdviceA {
@ExceptionHandler({SomeException.class})
public ResponseEntity<String> handleSomeException(SomeException pe, HandlerMethod handlerMethod) {
Class controllerClass = handlerMethod.getMethod().getDeclaringClass();
//controllerClass.toString will give you fully qualified name
return new ResponseEntity<>("SomeString", HttpStatus.BAD_REQUEST);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)