JSR303 data verification-2021 new version

first step

Import the jar package for data verification

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

Second step

Use annotations in entity classes

@Configuration
@ToString
@Data
@AllArgsConstructor
@NoArgsConstructor
@ConfigurationProperties(prefix = "haidong")
@Validated  //数据校验的注解
public class dog {
    
    
    private Integer id;
    private String name;
    @Email //校验你的邮箱格式是否正确
    private String email;
}

third step

Write value in yml file

haidong:
  id: 1
  name: haidong
  email: 123  --这里故意把邮箱格式写错 进行测试

the fourth step

test

Insert picture description here

We changed the email format to correct and tested

haidong:
  id: 1
  name: haidong
  email: 123@qq.com --这里写正确的邮箱格式

test

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45967375/article/details/115198661