赞
踩
目录
1、javax.validation
的一系列注解可以帮我们完成参数校验,免去繁琐的串行校验
不然我们的代码就像下面这样:
- // http://localhost:8080/api/user/save/serial
-
- /**
- * 走串行校验
- *
- * @param userVO
- * @return
- */
- @PostMapping("/save/serial")
- public Object save(@RequestBody UserVO userVO) {
- String mobile = userVO.getMobile();
-
- //手动逐个 参数校验~ 写法
- if (StringUtils.isBlank(mobile)) {
- return RspDTO.paramFail("mobile:手机号码不能为空");
- } else if (!Pattern.matches("^[1][3,4,5,6,7,8,9][0-9]{9}$", mobile)) {
- return RspDTO.paramFail("mobile:手机号码格式不对");
- }
-
- //抛出自定义异常等~写法
- if (StringUtils.isBlank(userVO.getUsername())) {
- throw new BizException(Constant.PARAM_FAIL_CODE, "用户名不能为空");
- }
-
- // 比如写一个map返回
- if (StringUtils.isBlank(userVO.getSex())) {
- Map<String, Object> result = new HashMap<>(5);
- result.put("code", Constant.PARAM_FAIL_CODE);
- result.put("msg", "性别不能为空");
- return result;
- }
- //.........各种写法 ...
- userService.save(userVO);
- return RspDTO.success();
- }
-

这被大佬看见,一定说,都9102了还这么写,然后被劝退了.....
- <!--jsr 303-->
- <dependency>
- <groupId>javax.validation</groupId>
- <artifactId>validation-api</artifactId>
- <version>1.1.0.Final</version>
- </dependency>
- <!-- hibernate validator-->
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-validator</artifactId>
- <version>5.2.0.Final</version>
- </dependency>
-
34_2.png
1、注解说明
- 1、@NotNull:不能为null,但可以为empty(""," "," ")
- 2、@NotEmpty:不能为null,而且长度必须大于0 (" "," ")
- 3、@NotBlank:只能作用在String上,不能为null,而且调用trim()后,长度必须大于0("test") 即:必须有实际字符
-
验证注解 | 验证的数据类型 | 说明 |
---|---|---|
@AssertFalse | Boolean,boolean | 验证注解的元素值是false |
@AssertTrue | Boolean,boolean | 验证注解的元素值是true |
@NotNull | 任意类型 | 验证注解的元素值不是null |
@Null | 任意类型 | 验证注解的元素值是null |
@Min(value=值) | BigDecimal,BigInteger, byte,short, int, long,等任何Number或CharSequence(存储的是数字)子类型 | 验证注解的元素值大于等于@Min指定的value值 |
@Max(value=值) | 和@Min要求一样 | 验证注解的元素值小于等于@Max指定的value值 |
@DecimalMin(value=值) | 和@Min要求一样 | 验证注解的元素值大于等于@ DecimalMin指定的value值 |
@DecimalMax(value=值) | 和@Min要求一样 | 验证注解的元素值小于等于@ DecimalMax指定的value值 |
@Digits(integer=整数位数, fraction=小数位数) | 和@Min要求一样 | 验证注解的元素值的整数位数和小数位数上限 |
@Size(min=下限, max=上限) | 字符串、Collection、Map、数组等 | 验证注解的元素值的在min和max(包含)指定区间之内,如字符长度、集合大小 |
@Past | java.util.Date,java.util.Calendar;Joda Time类库的日期类型 | 验证注解的元素值(日期类型)比当前时间早 |
@Future | 与@Past要求一样 | 验证注解的元素值(日期类型)比当前时间晚 |
@NotBlank | CharSequence子类型 | 验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的首位空格 |
@Length(min=下限, max=上限) | CharSequence子类型 | 验证注解的元素值长度在min和max区间内 |
@NotEmpty | CharSequence子类型、Collection、Map、数组 | 验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0) |
@Range(min=最小值, max=最大值) | BigDecimal,BigInteger,CharSequence, byte, short, int, long等原子类型和包装类型 | 验证注解的元素值在最小值和最大值之间 |
@Email(regexp=正则表达式,flag=标志的模式) | CharSequence子类型(如String) | 验证注解的元素值是Email,也可以通过regexp和flag指定自定义的email格式 |
@Pattern(regexp=正则表达式,flag=标志的模式) | String,任何CharSequence的子类型 | 验证注解的元素值与指定的正则表达式匹配 |
@Valid | 任何非原子类型 | 指定递归验证关联的对象如用户对象中有个地址对象属性,如果想在验证用户对象时一起验证地址对象的话,在地址对象上加@Valid注解即可级联验证 |
此处只列出Hibernate Validator提供的大部分验证约束注解,请参考hibernate validator官方文档了解其他验证约束注解和进行自定义的验证约束注解定义。
详细代码见——github.com/leaJone/mybot
这里我们在控制器层进行注解声明
- /**
- * 走参数校验注解
- *
- * @param userDTO
- * @return
- */
- @PostMapping("/save/valid")
- public RspDTO save(@RequestBody @Validated UserDTO userDTO) {
- userService.save(userDTO);
- return RspDTO.success();
- }
-
- import lombok.Data;
- import org.hibernate.validator.constraints.Length;
-
- import javax.validation.constraints.*;
- import java.io.Serializable;
- import java.util.Date;
-
- /**
- * @author LiJing
- * @ClassName: UserDTO
- * @Description: 用户传输对象
- */
- @Data
- public class UserDTO implements Serializable {
-
- private static final long serialVersionUID = 1L;
-
- /*** 用户ID*/
- @NotNull(message = "用户id不能为空")
- private Long userId;
-
- /** 用户名*/
- @NotBlank(message = "用户名不能为空")
- @Length(max = 20, message = "用户名不能超过20个字符")
- @Pattern(regexp = "^[\\u4E00-\\u9FA5A-Za-z0-9\\*]*$", message = "用户昵称限制:最多20字符,包含文字、字母和数字")
- private String username;
-
- /** 手机号*/
- @NotBlank(message = "手机号不能为空")
- @Pattern(regexp = "^[1][3,4,5,6,7,8,9][0-9]{9}$", message = "手机号格式有误")
- private String mobile;
-
- /**性别*/
- private String sex;
-
- /** 邮箱*/
- @NotBlank(message = "联系邮箱不能为空")
- @Email(message = "邮箱格式不对")
- private String email;
-
- /** 密码*/
- private String password;
-
- /*** 创建时间 */
- @Future(message = "时间必须是将来时间")
- private Date createTime;
-
- }
-

MethodArgumentNotValidException
是springBoot中进行绑定参数校验时的异常,需要在springBoot中处理,其他需要 处理ConstraintViolationException异常进行处理.
为了优雅一点,我们将参数异常,业务异常,统一做了一个全局异常,将控制层的异常包装到我们自定义的异常中
为了优雅一点,我们还做了一个统一的结构体,将请求的code,和msg,data一起统一封装到结构体中,增加了代码的复用性
- import com.boot.lea.mybot.dto.RspDTO;
- 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;
-
-
- /**
- * @author LiJing
- * @ClassName: GlobalExceptionHandler
- * @Description: 全局异常处理器
-
- */
- @RestControllerAdvice
- public class GlobalExceptionHandler {
-
- private Logger logger = LoggerFactory.getLogger(getClass());
-
- private static int DUPLICATE_KEY_CODE = 1001;
- private static int PARAM_FAIL_CODE = 1002;
- private static int VALIDATION_CODE = 1003;
-
- /**
- * 处理自定义异常
- */
- @ExceptionHandler(BizException.class)
- public RspDTO handleRRException(BizException e) {
- logger.error(e.getMessage(), e);
- return new RspDTO(e.getCode(), e.getMessage());
- }
-
- /**
- * 方法参数校验
- */
- @ExceptionHandler(MethodArgumentNotValidException.class)
- public RspDTO handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
- logger.error(e.getMessage(), e);
- return new RspDTO(PARAM_FAIL_CODE, e.getBindingResult().getFieldError().getDefaultMessage());
- }
-
- /**
- * ValidationException
- */
- @ExceptionHandler(ValidationException.class)
- public RspDTO handleValidationException(ValidationException e) {
- logger.error(e.getMessage(), e);
- return new RspDTO(VALIDATION_CODE, e.getCause().getMessage());
- }
-
- /**
- * ConstraintViolationException
- */
- @ExceptionHandler(ConstraintViolationException.class)
- public RspDTO handleConstraintViolationException(ConstraintViolationException e) {
- logger.error(e.getMessage(), e);
- return new RspDTO(PARAM_FAIL_CODE, e.getMessage());
- }
-
- @ExceptionHandler(NoHandlerFoundException.class)
- public RspDTO handlerNoFoundException(Exception e) {
- logger.error(e.getMessage(), e);
- return new RspDTO(404, "路径不存在,请检查路径是否正确");
- }
-
- @ExceptionHandler(DuplicateKeyException.class)
- public RspDTO handleDuplicateKeyException(DuplicateKeyException e) {
- logger.error(e.getMessage(), e);
- return new RspDTO(DUPLICATE_KEY_CODE, "数据重复,请检查后提交");
- }
-
-
- @ExceptionHandler(Exception.class)
- public RspDTO handleException(Exception e) {
- logger.error(e.getMessage(), e);
- return new RspDTO(500, "系统繁忙,请稍后再试");
- }
- }
-

如下文:确实做到了参数校验时返回异常信息和对应的code,方便了我们不再繁琐的处理参数校验
34_3.png
在ValidationMessages.properties 就是校验的message,有着已经写好的默认的message,且是支持i18n的,大家可以阅读源码赏析
- @Documented
- @Target({ElementType.PARAMETER, ElementType.FIELD})
- @Retention(RetentionPolicy.RUNTIME)
- @Constraint(validatedBy = IdentityCardNumberValidator.class)
- public @interface IdentityCardNumber {
-
- String message() default "身份证号码不合法";
-
- Class<?>[] groups() default {};
-
- Class<? extends Payload>[] payload() default {};
- }
-
这个注解是作用在Field字段上,运行时生效,触发的是IdentityCardNumber这个验证类。
这个是真正进行验证的逻辑代码:
- public class IdentityCardNumberValidator implements ConstraintValidator<IdentityCardNumber, Object> {
-
- @Override
- public void initialize(IdentityCardNumber identityCardNumber) {
- }
-
- @Override
- public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
- return IdCardValidatorUtils.isValidate18Idcard(o.toString());
- }
- }
-
IdCardValidatorUtils在项目源码中,可自行查看
- @NotBlank(message = "身份证号不能为空")
- @IdentityCardNumber(message = "身份证信息有误,请核对后提交")
- private String clientCardNo;
-
有的宝宝说同一个对象要复用,比如UserDTO在更新时候要校验userId,在保存的时候不需要校验userId,在两种情况下都要校验username,那就用上groups
了:
先定义groups的分组接口
Create
和Update
- import javax.validation.groups.Default;
-
- public interface Create extends Default {
- }
-
- import javax.validation.groups.Default;
-
- public interface Update extends Default{
- }
再在需要校验的地方
@Validated
声明校验组
- /**
- * 走参数校验注解的 groups 组合校验
- *
- * @param userDTO
- * @return
- */
- @PostMapping("/update/groups")
- public RspDTO update(@RequestBody @Validated(Update.class) UserDTO userDTO) {
- userService.updateById(userDTO);
- return RspDTO.success();
- }
-
在DTO中的字段上定义好
groups = {}
的分组类型
- @Data
- public class UserDTO implements Serializable {
-
- private static final long serialVersionUID = 1L;
-
- /*** 用户ID*/
- @NotNull(message = "用户id不能为空", groups = Update.class)
- private Long userId;
-
- /**
- * 用户名
- */
- @NotBlank(message = "用户名不能为空")
- @Length(max = 20, message = "用户名不能超过20个字符", groups = {Create.class, Update.class})
- @Pattern(regexp = "^[\\u4E00-\\u9FA5A-Za-z0-9\\*]*$", message = "用户昵称限制:最多20字符,包含文字、字母和数字")
- private String username;
-
- /**
- * 手机号
- */
- @NotBlank(message = "手机号不能为空")
- @Pattern(regexp = "^[1][3,4,5,6,7,8,9][0-9]{9}$", message = "手机号格式有误", groups = {Create.class, Update.class})
- private String mobile;
-
- /**
- * 性别
- */
- private String sex;
-
- /**
- * 邮箱
- */
- @NotBlank(message = "联系邮箱不能为空")
- @Email(message = "邮箱格式不对")
- private String email;
-
- /**
- * 密码
- */
- private String password;
-
- /*** 创建时间 */
- @Future(message = "时间必须是将来时间", groups = {Create.class})
- private Date createTime;
-
- }
-

在多个参数校验,或者@RequestParam 形式时候,需要在controller上加注@Validated
- @GetMapping("/get")
- public RspDTO getUser(@RequestParam("userId") @NotNull(message = "用户id不能为空") Long userId) {
- User user = userService.selectById(userId);
- if (user == null) {
- return new RspDTO<User>().nonAbsent("用户不存在");
- }
- return new RspDTO<User>().success(user);
- }
-
-
- @RestController
- @RequestMapping("user/")
- @Validated
- public class UserController extends AbstractController {
-
- ....圣洛代码...
-
-
用起来很简单,soEasy,重点参与的统一结构体返回,统一参数校验,是减少我们代码大量的try catch 的法宝,我觉得在项目中,将异常处理好,并将异常做好日志管理,才是很好的升华,文章浅显,只是一个菜鸟的进阶笔记....
这里只是个人见解,技术菜,欢迎大佬不吝赐教... 我是一个小白,技术在不断的更新迭代,我只有不断的填充自己的空白才能....跟上大佬们的步伐...
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。