一起来学SpringBoot | 第十九篇:轻松搞定数据验证(一)

42 篇文章 11 订阅
34 篇文章 2 订阅

SpringBoot是为了简化Spring应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程

对于任何一个应用而言,客户端做的数据有效性验证都不是安全有效的,而数据验证又是一个企业级项目架构上最为基础的功能模块,这时候就要求我们在服务端接收到数据的时候也对数据的有效性进行验证。为什么这么说呢?往往我们在编写程序的时候都会感觉后台的验证无关紧要,毕竟客户端已经做过验证了,后端没必要在浪费资源对数据进行验证了,但恰恰是这种思维最为容易被别人钻空子。毕竟只要有点开发经验的都知道,我们完全可以模拟HTTP请求到后台地址,模拟请求过程中发送一些涉及系统安全的数据到后台,后果可想而知….

为什么要轻松搞定?

相信通过上面的阅读,大家对数据验证的重要性有了一定的了解,那么为什么我这里要说轻松搞定呢?

下面这段代码很多人一定见到过,就是对参数进行有效性校验,但仔细观察的话就会发现;随着参数的增加,格式的变化,校验数据有效性的代码愈发的繁琐杂乱,一点都不轻松

public String test1(String name) {
    if (name == null) {
        throw new NullPointerException("name 不能为空");
    }
    if (name.length() < 2 || name.length() > 10) {
        throw new RuntimeException("name 长度必须在 2 - 10 之间");
    }
    return "success";
}

本章目标

通过Spring Boot完成参数后台数据校验,轻松搞定数据有效性验证,留出更多的时间来和小姐姐聊天…

具体代码

通过上面的阅读大家也大致能了解到为啥需要对异常进行全局捕获了,接下来就看看Spring Boot提供的解决方案

导入依赖

pom.xml中添加上spring-boot-starter-web的依赖即可

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

JSR-303 注释介绍

这里只列举了javax.validation包下的注解,同理在spring-boot-starter-web包中也存在hibernate-validator验证包,里面包含了一些javax.validation没有的注解,有兴趣的可以看看

注解说明
@NotNull限制必须不为null
@NotEmpty验证注解的元素值不为 null 且不为空(字符串长度不为0、集合大小不为0)
@NotBlank验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的空格
@Pattern(value)限制必须符合指定的正则表达式
@Size(max,min)限制字符长度必须在 min 到 max 之间(也可以用在集合上)
@Email验证注解的元素值是Email,也可以通过正则表达式和flag指定自定义的email格式
@Max(value)限制必须为一个不大于指定值的数字
@Min(value)限制必须为一个不小于指定值的数字
@DecimalMax(value)限制必须为一个不大于指定值的数字
@DecimalMin(value)限制必须为一个不小于指定值的数字
@Null限制只能为null(很少用)
@AssertFalse限制必须为false (很少用)
@AssertTrue限制必须为true (很少用)
@Past限制必须是一个过去的日期
@Future限制必须是一个将来的日期
@Digits(integer,fraction)限制必须为一个小数,且整数部分的位数不能超过 integer,小数部分的位数不能超过 fraction (很少用)

实体类

为了体现validation的强大,分别演示普通参数属性验证与对象的验证

package com.winterchen.pojo;

import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;

/**
 * Created by Donghua.Chen on 2018/7/24.
 */
public class Book {

    private Integer id;
    @NotBlank(message = "name 不允许为空")
    @Length(min = 2, max = 10, message = "name 长度必须在 {min} - {max} 之间")
    private String name;
    @NotNull(message = "price 不允许为空")
    @DecimalMin(value = "0.1", message = "价格不能低于 {value}")
    private BigDecimal price;


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }
}

控制层

与前面的代码相比,新的代码中仅仅多了几个注解而已。(此处只是为了图方便写在了 Controller 层,同理你可以将它作用在 Service 层)

注解介绍

  • @Validated:开启数据有效性校验,添加在类上即为验证方法,添加在方法参数中即为验证参数对象。(添加在方法上无效)
  • @NotBlank:被注释的字符串不允许为空(value.trim() > 0 ? true : false
  • @Length:被注释的字符串的大小必须在指定的范围内
  • @NotNull:被注释的字段不允许为空(value != null ? true : false)
  • @DecimalMin:被注释的字段必须大于或等于指定的数值
package com.winterchen.controller;

import com.winterchen.pojo.Book;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

/**
 * Created by Donghua.Chen on 2018/7/25.
 */
@Validated
@RestController
public class ValidateController {

    @GetMapping("/test2")
    public String test2(@NotBlank(message = "name 不能为空") @Length(min = 2, max = 10, message = "name 长度必须在 {min} - {max} 之间") String name) {
        return "success";
    }

    @GetMapping("/test3")
    public String test3(@Validated Book book) {
        return "success";
    }
}

主函数

package com.winterchen;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootValidation1Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootValidation1Application.class, args);
    }
}

测试

完成准备事项后,启动Chapter18Application自行测试即可,测试手段相信大伙都不陌生了,如浏览器postmanjunitswagger,此处基于postman,如果你觉得自带的异常信息不够友好,那么配上一起来学SpringBoot | 第十八篇:轻松搞定全局异常可以轻松搞定…

test2接口(name参数没传)

test2接口

test3接口(price参数值过低)

test3接口

总结

目前很多大佬都写过关于SpringBoot的教程了,如有雷同,请多多包涵,本教程基于最新的spring-boot-starter-parent:2.0.2.RELEASE编写,包括新版本的特性都会一起介绍…

说点什么

springboot技术交流群:681513531

个人博客:https://blog.winterchen.com

全文代码:https://github.com/WinterChenS/springboot-learning-experience

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值