介绍
JSR是Java Specification Requests的缩写,意思是Java 规范提案。是指向JCP(Java Community Process)提出新增一个标准化技术规范的正式请求。任何人都可以提交JSR,以向Java平台增添新的API和服务。JSR已成为Java界的一个重要标准。 简单来说,我们可以通过JSR303对前台发送的数据进行检查并判断是否符合逻辑。
快速上手
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.0.Final</version>
</dependency>
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Email;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Repository;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotNull;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Validated
@Repository
@ConfigurationProperties(prefix = "depart")
public class Department {
@Email
private Integer id;
private String name;
}
yaml
depart:
id: 1001
name: zxy
测试类
import com.zxy.demo.entity.Department;
import com.zxy.demo.entity.Employee;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
@Autowired
Department department;
@Test
public void test01(){
System.out.println(department);
}
}
程序此时不能正常启动  可知,我们设置的数据校验起了作用,其实数据校验本质就是设置正则表达式进行匹配。
常用注解
 Hibernate中补充的 
以上就是关于JSR303数据校验的简单讨论
|