作者:何甜甜在吗 \
起源:juejin.cn/post/6913735652806754311

前段时间提交代码审核,共事提了一个代码标准缺点:参数校验应该放在controller层。到底应该如何做参数校验呢

Controller层 VS Service层

去网上查阅了一些材料,个别举荐与业务无关的放在Controller层中进行校验,而与业务无关的放在Service层中进行校验。

那么如何将参数校验写的优雅好看呢,如果都是if - else,就感觉代码写的很low,还好有轮子能够应用

罕用校验工具类

应用Hibernate Validate

引入依赖

<dependency>    <groupId>org.hibernate</groupId>    <artifactId>hibernate-validator</artifactId>    <version>4.3.1.Final</version> </dependency>

罕用注解阐明

应用姿态

Spring Boot 根底就不介绍了,举荐下这个实战教程:
https://www.javastack.cn/cate...

须要搭配在Controller中搭配@Validated或@Valid注解一起应用,@Validated和@Valid注解区别不是很大,个别状况下任选一个即可,区别如下:

尽管@Validated比@Valid更加弱小,在@Valid之上提供了分组性能和验证排序功能,不过在理论我的项目中始终没有用到过

Hibernate-validate框架中的注解是须要加在实体中一起应用的

  • 定义一个实体
public class DataSetSaveVO {    //惟一标识符为空    @NotBlank(message = "user uuid is empty")    //用户名称只能是字母和数字    @Pattern(regexp = "^[a-z0-9]+$", message = "user names can only be alphabetic and numeric")    @Length(max = 48, message = "user uuid length over 48 byte")    private String userUuid;    //数据集名称只能是字母和数字    @Pattern(regexp = "^[A-Za-z0-9]+$", message = "data set names can only be letters and Numbers")    //文件名称过长    @Length(max = 48, message = "file name too long")    //文件名称为空    @NotBlank(message = "file name is empty")    private String name;    //数据集形容最多为256字节    @Length(max = 256, message = "data set description length over 256 byte")    //数据集形容为空    @NotBlank(message = "data set description is null")    private String description;}

阐明:message字段为不合乎校验规定时抛出的异样信息

  • Controller层中的办法
@PostMappingpublic ResponseVO createDataSet(@Valid @RequestBody DataSetSaveVO dataSetVO) {    return ResponseUtil.success(dataSetService.saveDataSet(dataSetVO));}

阐明:在校验的实体DataSetSaveVO旁边增加@Valid或@Validated注解

应用commons-lang3

引入依赖

<dependency>    <groupId>org.apache.commons</groupId>    <artifactId>commons-lang3</artifactId>    <version>3.4</version></dependency>

罕用办法阐明

测试代码

//StringUtils.isEmptySystem.out.println(StringUtils.isEmpty(""));  //trueSystem.out.println(StringUtils.isEmpty("  "));  //false//StringUtils.isNotEmptySystem.out.println(StringUtils.isNotEmpty(""));  //false        //StringUtils.isBlankSystem.out.println(StringUtils.isBlank(""));  //trueSystem.out.println(StringUtils.isBlank(" "));  //true//StringUtils.isNotBlankSystem.out.println(StringUtils.isNotBlank(" "));  //falseList<Integer> emptyList = new ArrayList<>();List<Integer> nullList = null;List<Integer> notEmptyList = new ArrayList<>();notEmptyList.add(1);//CollectionUtils.isEmptySystem.out.println(CollectionUtils.isEmpty(emptyList));   //trueSystem.out.println(CollectionUtils.isEmpty(nullList));   //trueSystem.out.println(CollectionUtils.isEmpty(notEmptyList));   //false//CollectionUtils.isNotEmptySystem.out.println(CollectionUtils.isNotEmpty(emptyList));   //falseSystem.out.println(CollectionUtils.isNotEmpty(nullList));   //falseSystem.out.println(CollectionUtils.isNotEmpty(notEmptyList));   //true

自定义注解

当下面的方面都无奈满足校验的需要当前,能够思考应用自定义注解。

近期热文举荐:

1.1,000+ 道 Java面试题及答案整顿(2021最新版)

2.别在再满屏的 if/ else 了,试试策略模式,真香!!

3.卧槽!Java 中的 xx ≠ null 是什么新语法?

4.Spring Boot 2.5 重磅公布,光明模式太炸了!

5.《Java开发手册(嵩山版)》最新公布,速速下载!

感觉不错,别忘了顺手点赞+转发哦!