spring boot 最佳实践(二)--使用Bean Validation
2017-05-24 14:20
615 查看
JavaBean定义了一组规则,JavaBean就是遵循此规则的平常的Java对象。
满足这三个条件:
1. JavaBean类必须有一个没有参数的构造函数。
2. JavaBean类所有的属性最好定义为私有的。
3. JavaBean类中定义函数setXxx()和getXxx()来对属性进行操作。其中Xxx是首字母大写的私有变量名称。
传统的代码从展现层、业务逻辑层、持久层、数据传输层分别有不同的对象。VO、BO、PO、DTO、POJO傻傻分不清楚。在不同层有不同的数据校验代码,编写大量的if else、code message、throw new Exception。
在spring data jpa编程范式中,Entity实体是一个简单Java对象,Repository用于对一个entity的重建。建议统一使用简单Java对象POJO定义Entity实体,减少VO、DTO定义,不要定义存储对象PO。
Bean Validation 为 JavaBean 验证定义了相应的元数据模型和 API。在应用程序中,通过使用 Bean Validation 或是你自己定义的 constraint,例如 @NotNull, @Max, @ZipCode, 就可以确保数据模型(JavaBean)的正确性。constraint 可以附加到字段,getter 方法,类或者接口上面。对于一些特定的需求,用户可以很容易的开发定制化的 constraint。Bean Validation 是一个运行时的数据验证框架,在验证之后验证的错误信息会被马上返回。
Hibernate Validator 5.x 是Bean Validation 1.1参考实现。hibernate validator官方文档提供了注解的列表。
如果想在代码中使用EL表达式还需引入。
定义一个javabean
执行单元测试
满足这三个条件:
1. JavaBean类必须有一个没有参数的构造函数。
2. JavaBean类所有的属性最好定义为私有的。
3. JavaBean类中定义函数setXxx()和getXxx()来对属性进行操作。其中Xxx是首字母大写的私有变量名称。
传统的代码从展现层、业务逻辑层、持久层、数据传输层分别有不同的对象。VO、BO、PO、DTO、POJO傻傻分不清楚。在不同层有不同的数据校验代码,编写大量的if else、code message、throw new Exception。
在spring data jpa编程范式中,Entity实体是一个简单Java对象,Repository用于对一个entity的重建。建议统一使用简单Java对象POJO定义Entity实体,减少VO、DTO定义,不要定义存储对象PO。
Bean Validation 为 JavaBean 验证定义了相应的元数据模型和 API。在应用程序中,通过使用 Bean Validation 或是你自己定义的 constraint,例如 @NotNull, @Max, @ZipCode, 就可以确保数据模型(JavaBean)的正确性。constraint 可以附加到字段,getter 方法,类或者接口上面。对于一些特定的需求,用户可以很容易的开发定制化的 constraint。Bean Validation 是一个运行时的数据验证框架,在验证之后验证的错误信息会被马上返回。
Hibernate Validator 5.x 是Bean Validation 1.1参考实现。hibernate validator官方文档提供了注解的列表。
表 1. 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) | 被注释的元素必须符合指定的正则表达式 |
表 2. Hibernate Validator 附加的 constraint
Constraint | 详细信息 |
---|---|
被注释的元素必须是电子邮箱地址 | |
@Length | 被注释的字符串的大小必须在指定的范围内 |
@NotEmpty | 被注释的字符串的必须非空 |
@Range | 被注释的元素必须在合适的范围内 |
项目搭建
首先添加hibernate validator 5依赖<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.4.1.Final</version> </dependency>
如果想在代码中使用EL表达式还需引入。
<dependency> <groupId>javax.el</groupId> <artifactId>javax.el-api</artifactId> <version>2.2.4</version> <scope>provided</scope> </dependency>
定义一个javabean
import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; public class Car { @NotNull private String manufacturer; @NotNull @Size(min = 2, max = 14) private String licensePlate; @Min(2) private int seatCount; public Car(String manufacturer, String licencePlate, int seatCount) { this.manufacturer = manufacturer; this.licensePlate = licencePlate; this.seatCount = seatCount; } //getters and setters ... }
执行单元测试
import java.util.Set; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.assertEquals; public class CarTest { private static Validator validator; @BeforeClass public static void setUpValidator() { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); validator = factory.getValidator(); } @Test public void manufacturerIsNull() { Car car = new Car( null, "DD-AB-123", 4 ); Set<ConstraintViolation<Car>> constraintViolations = validator.validate( car ); assertEquals( 1, constraintViolations.size() ); assertEquals( "may not be null", constraintViolations.iterator().next().getMessage() ); } }
在spring mvc中使用 bean validation
spring mvc4.0支持 bean validation1.1,可以在controller中使用@Valid指定要校验的实体类。//import javax.validation.Valid; @RequestMapping("/demo" a5a4 ) public String demo(@Valid Demo demo,Model model){ // do something }
相关文章推荐
- springboot 整合dubbo最佳实践 (使用redis作为注册中心)
- DubboX + SpringBoot最佳实践
- SpringBoot JMS(ActiveMQ) 使用实践
- SpringBoot学习(八)--SpringBoot中Restful最佳实践
- Spring Boot 最佳实践(二)集成Jsp与生产环境部署
- Spring Boot 实践折腾记(三):三板斧,Spring Boot下使用Mybatis
- 实力干货!SpringBoot JMS(ActiveMQ) 使用实践
- springboot缓存的使用实践
- SpringBoot实践之---定时任务scheduled的使用
- Spring Boot系列——AOP配自定义注解的最佳实践
- spring boot 最佳实践(四)--Spring MVC
- 【Rainbond最佳实践】Spring Boot框架配置MySQL
- 使用Swagger生成Spring Boot REST客户端(支持Feign)(待实践)
- paip.spring3 mvc servlet的配置以及使用最佳实践
- spring boot 最佳实践(五)--SpEL
- spring boot 最佳实践(九)-- 数据访问权限
- spring boot 最佳实践(八)-- 请求上下文注入
- Spring Boot 最佳实践(三)模板引擎FreeMarker集成
- SpringBoot实践之(十五)---自定义注解Annotation的使用