在SpringBoot中,我们使用@ControllerAdvice注解定义全局异常处理器。详见下方代码块:
@ControllerAdvice
public class GlobalExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(MissingServletRequestParameterException.class)
@ResponseBody
public GeneralResponse handleMissingServletRequestParameterException(MissingServletRequestParameterException e,
HttpServletRequest request) {
String message = "缺失请求参数" + e.getParameterName();
return ackTransfer(request, message, HttpStatus.BAD_REQUEST.value(), e);
}
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
@ResponseBody
public GeneralResponse handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e,
HttpServletRequest request) {
String message = "请求参数" + e.getName() + "类型错误";
return ackTransfer(re