标签:music integer 方式 https max 表达式 不为 规范 imp
Bean Validation 中的 constraint
| Constraint | 详细信息 |
|---|---|
| @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) | 被注释的元素必须符合指定的正则表达式 |
| Constraint | 详细信息 |
|---|---|
| 被注释的元素必须是电子邮箱地址 | |
| @Length | 被注释的字符串的大小必须在指定的范围内 |
| @NotEmpty | 被注释的字符串的必须非空 |
| @Range | 被注释的元素必须在合适的范围内 |
一个 constraint 通常由 annotation 和相应的 constraint validator 组成,它们是一对多的关系。也就是说可以有多个 constraint validator 对应一个 annotation。在运行时,Bean Validation 框架本身会根据被注释元素的类型来选择合适的 constraint validator 对数据进行验证。
有些时候,在用户的应用中需要一些更复杂的 constraint。Bean Validation 提供扩展 constraint 的机制。可以通过两种方法去实现,一种是组合现有的 constraint 来生成一个更复杂的 constraint,另外一种是开发一个全新的 constraint。
Person 类
package Itd.kittenplus.mall.pojo;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
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.Max;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @author 李嘉俊
* @create 2020-03-24 23:39
* @desc 测试 JSR303
**/
@Data
@Component
/**
* Spring-Boot 提供@ConfigurationProperties注解将 person配置文件的值映射到类上使用
*/
@ConfigurationProperties(prefix = "person")
/**
* 数据校验
*/
@Validated
public class Person {
@Email()
/**
* @Email(message="输入你想输入的文字")
*/
private String name;
@NotNull(message="年龄不能为空")
@Max(value=100,message = "年龄最大不能超过一百岁")
private Integer age;
private Boolean happy;
private Date birth;
private Map<String, Object> maps;
private List<Object> lists;
}
配置类
person:
name: zhangsan
age: ${random.int}
birth: 2019/2/4
maps: {k1: v1,k2: v2}
hello: if i can go
lists:
- code
- music
- girl
测试类
package Itd.kittenplus.mall;
import Itd.kittenplus.mall.pojo.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class KittenplusMallApplicationTests {
@Autowired
private Person person;
@Test
public void contextLoads() {
System.out.println(person);
}
}
由于配置类中Email 邮箱格式错误,则报错

修改配置类中邮箱格式后,如图:

本文知识原理参考至: https://www.jianshu.com/p/48725b7328c9
标签:music integer 方式 https max 表达式 不为 规范 imp
原文地址:https://www.cnblogs.com/thinkAboutMore/p/12563398.html