SpringBoot全局异常处理

  • 异常处理类
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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值