SpringBoot数据校验与优雅处理详解

本文详述了SpringBoot中后端参数校验的重要性,介绍了使用Validator框架和@Valid/@Validated注解进行数据校验的方法,包括自定义约束注解的创建。同时,讲解了如何通过全局异常处理优雅地处理校验失败的情况,确保系统安全性和用户体验。
摘要由CSDN通过智能技术生成

本篇要点

JDK1.8、SpringBoot2.3.4release

  • 说明后端参数校验的必要性。
  • 介绍如何使用validator进行参数校验
  • 介绍@Valid和@Validated的区别。
  • 介绍如何自定义约束注解
  • 关于Bean Validation的前世今生

后端参数校验的必要性

在开发中,从表现层到持久化层,数据校验都是一项逻辑差不多,但容易出错的任务,

前端框架往往会采取一些检查参数的手段,比如校验并提示信息,那么,既然前端已经存在校验手段,后端的校验是否还有必要,是否多余了呢?

并不是,正常情况下,参数确实会经过前端校验传向后端,但如果后端不做校验,一旦通过特殊手段越过前端的检测,系统就会出现安全漏洞。

不使用Validator的参数处理逻辑

既然是参数校验,很简单呀,用几个if/else直接搞定:

    @PostMapping("/form")
    public String form(@RequestBody Person person) {
        if (person.getName() == null) {
            return "姓名不能为null";
        }
        if (person.getName().length() < 6 || person.getName().length() > 12) {
            return "姓名长度必须在6 - 12之间";
        }
        if (person.getAge() == null) {
            return "年龄不能为null";
        }
        if (person.getAge() < 20) {
            return "年龄最小需要20";
        }
        // service ..
        return "注册成功!";
    }

写法干脆,但if/else太多,过于臃肿,更何况这只是区区一个接口的两个参数而已,要是需要更多参数校验,甚至更多方法都需要这要的校验,这代码量可想而知。于是,这种做法显然是不可取的,我们可以利用下面这种更加优雅的参数处理方式。

Validator框架提供的便利

Validating data is a common task that occurs throughout all application layers, from the presentation to the persistence layer. Often the same validation logic is implemented in each layer which is time consuming and error-prone.

如果依照下图的架构,对每个层级都进行类似的校验,未免过于冗杂。

SpringBoot数据校验与优雅处理详解

 

Jakarta Bean Validation 2.0 - defines a metadata model and API for entity and method validation. The default metadata source are annotations, with the ability to override and extend the meta-data through the use of XML.

The API is not tied to a specific application tier nor programming model. It is specifically not tied to either web or persistence tier, and is available for both server-side application programming, as well as rich client Swing application developers.

Jakarta Bean Validation2.0定义了一个元数据模型,为实体和方法提供了数据验证的API,默认将注解作为源,可以通过XML扩展源。

SpringBoot数据校验与优雅处理详解

 

SpringBoot自动配置ValidationAutoConfiguration

Hibernate Validator是Jakarta Bean Validation的参考实现。

在SpringBoot中,只要类路径上存在JSR-303的实现,如Hibernate Validator,就会自动开启Bean Validation验证功能,这里我们只要引入spring-boot-starter-validation的依赖,就能完成所需。

        <dependenc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值