Commit 8d948e0a by 袁伟铭

优化代码

parent 24faa4bc
...@@ -60,12 +60,6 @@ public class UnifiedExceptionHandler { ...@@ -60,12 +60,6 @@ public class UnifiedExceptionHandler {
return ResultVo.fail(ex.getCode(), errMessage); return ResultVo.fail(ex.getCode(), errMessage);
} }
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResultVo handleArgumentNotValidException(MethodArgumentNotValidException ex, HttpServletRequest request) {
log.warn(">> argument not valid error: {} {}", request.getRequestURI(), ex.getMessage());
return ResultVo.fail(HttpStatus.BAD_REQUEST.value(), "无效的请求参数" + ex.getParameter().getParameterName());
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResultVo handleMethodNotSupportedException(HttpRequestMethodNotSupportedException ex, HttpServletRequest request) { public ResultVo handleMethodNotSupportedException(HttpRequestMethodNotSupportedException ex, HttpServletRequest request) {
log.warn(">> method not supported error: {} {}, expected: {}", log.warn(">> method not supported error: {} {}, expected: {}",
...@@ -91,6 +85,17 @@ public class UnifiedExceptionHandler { ...@@ -91,6 +85,17 @@ public class UnifiedExceptionHandler {
return ResultVo.fail(HttpStatus.BAD_REQUEST.value(), "无法解析请求消息"); return ResultVo.fail(HttpStatus.BAD_REQUEST.value(), "无法解析请求消息");
} }
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResultVo handleArgumentNotValidException(MethodArgumentNotValidException ex, HttpServletRequest request) {
log.warn(">> argument not valid error: {} {}", request.getRequestURI(), ex.getMessage());
String paranmName = ex.getParameter().getParameterName();
List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
if (CollUtil.isNotEmpty(fieldErrors)) {
paranmName = fieldErrors.stream().map(f -> "[" + f.getDefaultMessage() + "(" + f.getField() + ")]").collect(Collectors.joining("、"));
}
return ResultVo.fail(HttpStatus.BAD_REQUEST.value(), "无效的请求参数:" + paranmName);
}
@ExceptionHandler({MissingServletRequestPartException.class, MissingServletRequestParameterException.class, BindException.class}) @ExceptionHandler({MissingServletRequestPartException.class, MissingServletRequestParameterException.class, BindException.class})
public ResultVo handleMissingServletRequestPartException(Exception ex, HttpServletRequest request) { public ResultVo handleMissingServletRequestPartException(Exception ex, HttpServletRequest request) {
log.warn(">> missing servlet request part/param error: {} {}", request.getRequestURI(), ex.getMessage()); log.warn(">> missing servlet request part/param error: {} {}", request.getRequestURI(), ex.getMessage());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment