首页 > 其他分享 >SpringBoot(五):JSR303校验

SpringBoot(五):JSR303校验

时间:2022-12-14 12:23:13浏览次数:44  
标签:SpringBoot JSR303 age 校验 public 注释 import email name

一、什么是JSR303校验

JSR,是Java Specification Requests的缩写,意思是Java规范提案,是指向JCP(Java Community Process)提出新增一个标准化技术规范的正式请求。

JCP(Java Community Process)是一个开放的国际组织,主要由Java开发者以及被授权者组成,职能是发展和更新。

也就是说JSR303其实就是一种Java规范,它叫做Bean Validation,意思就是对Bean进行验证。

二、JSR303的校验类型

内置注解:
@Null 被注释的元素必须为 null
@NotNull 被注释的元素必须不为 null
@AssertTrue 被注释的元素必须为 true
@AssertFalse 被注释的元素必须为 false
@Min(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@Max(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@DecimalMin(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@Size(max, min) 被注释的元素的大小必须在指定的范围内
@Digits (integer, fraction) 被注释的元素必须是一个数字,其值必须在可接受的范围内
@Past 被注释的元素必须是一个过去的日期
@Future 被注释的元素必须是一个将来的日期
@Pattern(value) 被注释的元素必须符合指定的正则表达式
Hibernate扩展注解:
@Email 被注释的元素必须是电子邮箱地址
@Length 被注释的字符串的大小必须在指定的范围内
@NotEmpty 被注释的字符串的必须非空
@Range 被注释的元素必须在合适的范围内

三、SpringBoot中JSR303校验的简单使用

1.依赖导入

     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

2.注解使用

Bean中使用@Validated注解,在对应的属性上使用JSR303中的注解,示例如下:

package com.jms.config;

import com.sun.istack.internal.NotNull;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Email;
import javax.validation.constraints.Min;

@Validated
@Component
@ConfigurationProperties(prefix = "jsr303")
public class TestJSR303 {

    @NotNull
    private String name;
    @Min(0)
    private int age;

    @Email
    private String email;

    public TestJSR303() {
    }

    public TestJSR303(String name, int age, String email) {
        this.name = name;
        this.age = age;
        this.email = email;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "TestJSR303{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", email='" + email + '\'' +
                '}';
    }
}

yaml文件内容:

jsr303:
  name: jsr
  age: -1
  email: aaa

3.测试:

package com.jms;

import com.jms.config.TestJSR303;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Springboot02ConfigApplicationTests {
    private TestJSR303 testConfig;
    @Autowired
    public void setTestConfig(TestJSR303 testConfig) {
        this.testConfig = testConfig;
    }
    @Test
    void contextLoads() {
        System.out.println(testConfig);
    }

}

测试结果如下:

 

标签:SpringBoot,JSR303,age,校验,public,注释,import,email,name
From: https://www.cnblogs.com/jmsstudy/p/16981562.html

相关文章

  • springboot依靠注解的定时任务与hutool的定时任务
    在学习hutool文档时发现它也有一个定时任务,怎么实现的呢?resources目录下创建一个config目录,config目录下创建cron.setting文件[com.fzg.common.demo.uuid]UUIDdemo......
  • java自定义注解实现前后台参数校验
     其实是可以通过@Constraint来限定自定义注解的方法。@Constraint(validatedBy=xxxx.class) 下面是我做的java自定义注解实现前后台参数校验的代码示例对这个感兴趣的......
  • 在springboot使用jsp
    在springboot配置jsp环境在pom.xml中添加配置依赖内容如下:<dependency><groupId>org.springframework.boot</groupId><artifactId>sp......
  • springboot上传图片
    springboot上传图片新建一个springboot项目;在java/main/com/ljx创建一个controller.fileController类内容如下:packagecom.ljx.controller;​importorg.spr......
  • nginx反向代理部署springboot项目报404无法加载静态资源
    问题:nginx反向代理部署springboot项目报404无法加载静态资源(css,js,jpg,png...)为什么要用反向代理:springboot默认启动端口为8080,如果需要通过域名(不加端口号)直接访问sprin......
  • Java 基于springBoot 上传文件
    @RequestMapping("/uploadFile")@ResponseBodypublicMap<String,Object>uploadFile(@RequestParam(value="file")MultipartFile[]file,HttpServletRequ......
  • java springboot项目树结构递归查询
    记录工作本文记录树结构递归查询,像菜单栏和部门首先需要一张表CREATETABLE`sys_dict`(`id`intNOTNULLAUTO_INCREMENT,`parent_id`intNOTNULL,`name`......
  • 前端常用的正则校验
     例如element常用的必填校验:   固话和手机号pattern:/^(((\d{3,4}-)?[0-9]{7,8})|(1(3|4|5|6|7|8|9)\d{9}))$/ 手机号pattern:/^1[3|4|5|6|7|8|9][0-......
  • java springboot+rabbitmq+websocket 订阅展示
    记录工作需要的依赖<!--fastjson坐标--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId>......
  • SpringBoot2 静态文件路径与接口路径冲突(相同)解决方案
    事情是这样的,最近接手个项目给它底层从ssm整到springboot2+mp由于之前很多xxx.do请求而我又不想用后缀,所以就得匹配全部后缀或者无后缀(方法有很多方案自行百度)......