Springboot中优雅进行字段校验

欢迎关注方志朋的博客,回复”666“获面试宝典

前段时间提交代码审核,同事提了一个代码规范缺陷:参数校验应该放在controller层。到底应该如何做参数校验呢?

| Controller层 VS Service层

去网上查阅了一些资料,一般推荐与业务无关的放在Controller层中进行校验,而与业务有关的放在Service层中进行校验。

那么如何将参数校验写的优雅美观呢,如果都是if - else,就感觉代码写的很low,还好有轮子可以使用。

| 常用校验工具类

使用Hibernate Validate

引入依赖


    org.hibernate
    hibernate-validator
    4.3.1.Final 

常用注解说明

Springboot中优雅进行字段校验_第1张图片

使用姿势

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

Springboot中优雅进行字段校验_第2张图片

虽然@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层中的方法:

@PostMapping
public ResponseVO createDataSet(@Valid @RequestBody DataSetSaveVO dataSetVO) {
    return ResponseUtil.success(dataSetService.saveDataSet(dataSetVO));
}

说明:在校验的实体DataSetSaveVO旁边添加@Valid或@Validated注解。

使用commons-lang3

引入依赖


    org.apache.commons
    commons-lang3
    3.4

常用方法说明

Springboot中优雅进行字段校验_第3张图片

测试代码

//StringUtils.isEmpty
System.out.println(StringUtils.isEmpty(""));  //true
System.out.println(StringUtils.isEmpty("  "));  //false
//StringUtils.isNotEmpty
System.out.println(StringUtils.isNotEmpty(""));  //false
        
//StringUtils.isBlank
System.out.println(StringUtils.isBlank(""));  //true
System.out.println(StringUtils.isBlank(" "));  //true
//StringUtils.isNotBlank
System.out.println(StringUtils.isNotBlank(" "));  //false

List emptyList = new ArrayList<>();
List nullList = null;
List notEmptyList = new ArrayList<>();
notEmptyList.add(1);

//CollectionUtils.isEmpty
System.out.println(CollectionUtils.isEmpty(emptyList));   //true
System.out.println(CollectionUtils.isEmpty(nullList));   //true
System.out.println(CollectionUtils.isEmpty(notEmptyList));   //false

//CollectionUtils.isNotEmpty
System.out.println(CollectionUtils.isNotEmpty(emptyList));   //false
System.out.println(CollectionUtils.isNotEmpty(nullList));   //false
System.out.println(CollectionUtils.isNotEmpty(notEmptyList));   //true

| 自定义注解

当上面的方面都无法满足校验的需求以后,可以考虑使用自定义注解。如何写一个自定义注解,可以参考本文作者之前的文章:《Spring自定义注解从入门到精通》。

转自:何甜甜在吗

链接:https://juejin.cn/post/6913735652806754311

热门内容:
  • mybatis-plus团队新作:mybatis-mate 轻松搞定数据权限

  • 面试能力者当场拿offer,Java基础天花板实锤了!

  • 牛逼,国产开源的远程桌面火了,只有9MB,支持自建中继器!

  • 吃透这“ 16个 ”核心技术栈,月薪3W随便叫!

  • 不满月薪12000辞职,跳槽直接进大厂,据说背了很多面试八股文?!

Springboot中优雅进行字段校验_第4张图片


最近面试BAT,整理一份面试资料《Java面试BAT通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。
获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。

明天见(。・ω・。)ノ♡

你可能感兴趣的:(java,spring,spring,boot,面试,大数据)