import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
@RestControllerAdvice
public class GlobalExceptionHandler {
class AjaxReult{
private Integer code;
private String msg;
public AjaxReult(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
}
private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(ValidationException.class)
public AjaxReult handleValidationException(ValidationException e) {
LOGGER.error(e.getMessage(), e);
return new AjaxReult(1, e.getCause().getMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public AjaxReult handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
LOGGER.error(e.getMessage(), e);
return new AjaxReult(1, e.getBindingResult().getFieldError().getDefaultMessage());
}
@ExceptionHandler(ConstraintViolationException.class)
public AjaxReult handleConstraintViolationException(ConstraintViolationException e) {
LOGGER.error(e.getMessage(), e);
return new AjaxReult(1, e.getMessage());
}
@ExceptionHandler(NoHandlerFoundException.class)
public AjaxReult handlerNoFoundException(Exception e) {
LOGGER.error(e.getMessage(), e);
return new AjaxReult(404, "路径不存在,请检查路径是否正确");
}
@ExceptionHandler(DuplicateKeyException.class)
public AjaxReult handleDuplicateKeyException(DuplicateKeyException e) {
LOGGER.error(e.getMessage(), e);
return new AjaxReult(1, "数据重复,请检查后提交");
}
@ExceptionHandler(Exception.class)
public AjaxReult handleException(Exception e) {
LOGGER.error(e.getMessage(), e);
return new AjaxReult(500, "系统繁忙,请稍后再试");
}
}
implementation group: 'org.hibernate', name: 'hibernate-validator', version: '6.1.7.Final'
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.io.Serializable;
public class PswLoginVO implements Serializable {
@NotBlank(message = "phoneNo must not be blank")
@Size(max = 13, min = 13, message = "wrong phoneNo length")
@Pattern(regexp = "^(861)\\d{10}$", message = "wrong phoneNo pattern")
private String phoneNo;
@NotBlank(message = "password must not be blank")
private String password;
public PswLoginVO() {}
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "PswLoginVO{" + "phoneNo='" + phoneNo + '\'' + ", password='" + password + '\'' + '}';
}
}
- 在接口入参位置添加@Validated,即可检验入参
@RequestBody @Validated PswLoginVO pswLoginVO
- 对象嵌套情况下,在内部对象上加@validated注解,
List<Object>
还需要在Controller类上加@validated