基于注解的全局异常处理
全局异常处理几大元素----针对控制层
服务层抛出异常,控制层不捕捉,就会被此全局异常处理捕获
1,@controllerAdvice 控制层的通知---里面的属性值annotations可以设置其对哪种类切面通知 比如controller.class-----对那些类捕捉异常
2,@ExceptionHAndle 里面的value属性可以执行对哪种异常捕获,遇到这种异常就这个对应的方法处理--------对哪种异常捕获
@ControllerAdvice(annotations = RestController.class)
@Slf4j
public class RestExceptionHandler {
@Autowired
private TokenUtils tokenUtils;
@ExceptionHandler
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)
private ResultMap commonExceptionHandler(HttpServletRequest request, Exception e) {
e.printStackTrace();
log.error(e.getMessage());
return new ResultMap(tokenUtils).failAndRefreshToken(request).message(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
}
@ExceptionHandler(value = ServerException.class)
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)
private ResultMap serverExceptionHandler(HttpServletRequest request, Exception e) {
e.printStackTrace();
log.error(e.getMessage());
return new ResultMap(tokenUtils).failAndRefreshToken(request).message(e.getMessage());
}
}
上一篇: mysql date