validate分组校验

Spring JSR

<dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
</dependency>

下面进行分组校验的学习
自己新建接口,继承Default和不继承Default,区别就是继承Default的分组就是使用当前分组的时候,默认分组下的校验都会生效,如@NotNull @NotBlank 类似这种

public interface UpdateElement {
}
public interface Add  extends Default {
}

什么时候校验,报什么错误信息呢,这是在与前后端交互的VO中,在属性上标注哪个组的校验规格是什么

package com.bjq.vo;


import com.bjq.validate.UpdateElement;
import lombok.Data;

import javax.validation.constraints.NotBlank;

@Data
public class RegisterVO {
    @NotBlank(message = "用户名不能为空",groups = {UpdateElement.class})
    private String account;

    @NotBlank
    private String password;

    private Integer userType;

    private String name;

    private String phone;

    private Integer grade;

    private String sdept;
}

在上面的例子中account属性的校验就是属于UpdateElement组的校验,而password属于Default分组的校验,

 @PostMapping("/register")
    public Resp register(@Validated @RequestBody RegisterVO registerVO) {
        return userService.register(registerVO);
    }

这个情况下,@Validated使用的是默认分组,这时候不会对account属性进行校验

@PostMapping("/register")
    public Resp register(@Validated(UpdateElement.class) @RequestBody RegisterVO registerVO) {
        return userService.register(registerVO);
    }

这个情况下,指定了UpdateElement分组,因为UpdateElement分组没有继承Default ,所以只会校验account 不会校验 password

最后针对这种优雅参数校验配置异常处理

[org.springframework.web.bind.MethodArgumentNotValidException:

package com.bjq.interceptor;


import cn.dev33.satoken.exception.NotLoginException;
import com.bjq.base.ErrorCode;
import com.bjq.base.Resp;
import com.bjq.exception.BussinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestControllerAdvice
public class ExceptionHande {
    private  final  String  COMMOM_EXCEPTION = "未知异常";

   @ExceptionHandler(value = BussinessException.class)
    public Resp<String> businessExceptionHandler(BussinessException bs , HttpServletResponse response){
       log.error("业务异常",bs);
       ErrorCode errorCode  = bs.getErrorCode();
       return Resp.error(errorCode.getCode(),bs.getMessage());
   }

    @ExceptionHandler(value = NotLoginException.class)
    public Resp<String> notLoginExceptionHandler(NotLoginException bs , HttpServletResponse response){
        log.error("业务异常",bs);
        return Resp.error(-7,"用户未登录");
    }
   @ExceptionHandler(value = RuntimeException.class)
    public Resp<String> runtimeExceptionHandler(RuntimeException runtimeException){
       log.error("运行报错",runtimeException);
       return  Resp.error(-2,COMMOM_EXCEPTION);
   }

   @ExceptionHandler(MethodArgumentNotValidException.class)
    public Resp argsException(MethodArgumentNotValidException me){
       return Resp.error(-999,"参数有误");
   }
}

如上所示,当validated异常的时候,就会拦截并报 999 异常。

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值