使用全局异常处理检验逻辑
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package com.macro.mall.common.exception;
|
||||
|
||||
import com.macro.mall.common.api.CommonResult;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
@@ -20,4 +24,32 @@ public class GlobalExceptionHandler {
|
||||
}
|
||||
return CommonResult.failed(e.getMessage());
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@ExceptionHandler(value = MethodArgumentNotValidException.class)
|
||||
public CommonResult handleValidException(MethodArgumentNotValidException e) {
|
||||
BindingResult bindingResult = e.getBindingResult();
|
||||
String message = null;
|
||||
if (bindingResult.hasErrors()) {
|
||||
FieldError fieldError = bindingResult.getFieldError();
|
||||
if (fieldError != null) {
|
||||
message = fieldError.getField()+fieldError.getDefaultMessage();
|
||||
}
|
||||
}
|
||||
return CommonResult.validateFailed(message);
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@ExceptionHandler(value = BindException.class)
|
||||
public CommonResult handleValidException(BindException e) {
|
||||
BindingResult bindingResult = e.getBindingResult();
|
||||
String message = null;
|
||||
if (bindingResult.hasErrors()) {
|
||||
FieldError fieldError = bindingResult.getFieldError();
|
||||
if (fieldError != null) {
|
||||
message = fieldError.getField()+fieldError.getDefaultMessage();
|
||||
}
|
||||
}
|
||||
return CommonResult.validateFailed(message);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user