Java SpringBoot 实体类数据自动验证
package demo.dto; import org.hibernate.validator.constraints.Length; import javax.validation.constraints.NotEmpty;
import java.io.Serializable; public class ProductDto implements Serializable {
@NotEmpty(message = "姓名 不允许为空")
@Length(min = 2, max = 10, message = "姓名 长度必须在 {min} - {max} 之间")
private String userName; @NotEmpty(message = "密码 不允许为空")
private String password; @NotEmpty(message = "真实姓名 不允许为空")
private String realName; public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password == null ? null : password.trim();
} public String getRealName() {
return realName;
} public void setRealName(String realName) {
this.realName = realName == null ? null : realName.trim();
}
/**
*
@NotEmpty,@NotNull和@NotBlank的区别
1 @NotEmpty :不能为null,且Size>0 2 @NotNull:不能为null,但可以为empty,没有Size的约束 3 @NotBlank:只用于String,不能为null且trim()之后size>0
*
@NotNull
使用该注解的字段的值不能为null,否则验证无法通过。 @Null
修饰的字段在验证时必须是null,否则验证无法通过。 @Size
如下代码表示,修饰的字段长度不能超过5或者低于。 @Size(min = 1, max = 5)
private String name;
1
2
@Max
如下代码表示,该字段的最大值为19,否则无法通过验证。
@Max(value = 19)
private Integer age;
1
2
@Min
同理,被该注解修饰的字段的最小值,不能低于某个值。 @AssertFalse
该字段值为false时,验证才能通过。 @AssertTrue
该字段值为true时,验证才能通过。 @DecimalMax
验证小数的最大值。 @DecimalMax(value = "12.35")
private double money;
1
2
@DecimalMin
验证小数的最小值。 @Digits
验证数字的整数位和小数位的位数是否超过指定的长度。 @Digits(integer = 2, fraction = 2)
private double money;
1
2
@Future
验证日期是否在当前时间之后,否则无法通过校验。
@Future
private Date date;
1
2
@Past
验证日期是否在当前时间之前,否则无法通过校验。 @Pattern
用于验证字段是否与给定的正则相匹配。 @Pattern(regexp = "[abc]")
private String name;
*/ }
package demo.entity;
import java.io.Serializable;
public class Product implements Serializable {
private Integer id;
private String userName;
private String password;
private String realName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName == null ? null : realName.trim();
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", username='" + userName + '\'' +
", password='" + password + '\'' +
", realname='" + realName + '\'' +
'}';
}
}
//添加数据
@RequestMapping("/addproduct")
public Object addproduct(@Valid ProductDto model, BindingResult result) {
int errorCount = result.getErrorCount();
MessagePack messagePack = new MessagePack();
// 验证字段是否符合规则
if (result.hasErrors()) {
throw new RuntimeException(result.getFieldError().getDefaultMessage());
} else {
Product product = new Product();
BeanUtils.copyProperties(model, product);
// 操作数据
int i = Convert.toInt(productService.addProduct(product));
// 判断操作成功与否
if (i > 0) {
messagePack.setCode(0);
messagePack.setMessage("新增商品成功");
messagePack.setObject(null);
messagePack.setStatus("OK");
} else {
messagePack.setCode(-1);
messagePack.setMessage("新增商品失败");
messagePack.setObject(null);
messagePack.setStatus("error");
}
}
return messagePack;
}
Java SpringBoot 实体类数据自动验证的更多相关文章
- Java SpringBoot 如何使用 IdentityServer4 作为验证学习笔记
这边记录下如何使用IdentityServer4 作为 Java SpringBoot 的 认证服务器和令牌颁发服务器.本人也是新手,所以理解不足的地方请多多指教.另外由于真的很久没有写中文了,用词不 ...
- SpringBoot注解把配置文件自动映射到属性和实体类实战
SpringBoot注解把配置文件自动映射到属性和实体类实战 简介:讲解使用@value注解配置文件自动映射到属性和实体类 1.配置文件加载 方式一 1.Controller上面配置 @Propert ...
- EBS OAF开发中的Java 实体对象(Entity Object)验证功能补充
EBS OAF开发中的Java 实体对象(Entity Object)验证功能补充 (版权声明,本人原创或者翻译的文章如需转载,如转载用于个人学习,请注明出处:否则请与本人联系,违者必究) EO理论上 ...
- java框架之SpringBoot(5)-SpringMVC的自动配置
本篇文章内容详细可参考官方文档第 29 节. SpringMVC介绍 SpringBoot 非常适合 Web 应用程序开发.可以使用嵌入式 Tomcat,Jetty,Undertow 或 Netty ...
- 【转载】JAVA SpringBoot 项目打成jar包供第三方引用自动配置(Spring发现)解决方案
JAVA SpringBoot 项目打成jar包供第三方引用自动配置(Spring发现)解决方案 本文为转载,原文地址为:https://www.cnblogs.com/adversary/p/103 ...
- SpringBoot整合Apache Shiro权限验证框架
比较常见的权限框架有两种,一种是Spring Security,另一种是Apache Shiro,两种框架各有优劣,个人感觉Shiro更容易使用,更加灵活,也更符合RABC规则,而且是java官方更推 ...
- 使用SpringBoot进行优雅的数据验证
JSR-303 规范 在程序进行数据处理之前,对数据进行准确性校验是我们必须要考虑的事情.尽早发现数据错误,不仅可以防止错误向核心业务逻辑蔓延,而且这种错误非常明显,容易发现解决. JSR303 规范 ...
- java~springboot~目录索引
回到占占推荐博客索引 最近写了不过关于java,spring,微服务的相关文章,今天把它整理一下,方便大家学习与参考. java~springboot~目录索引 Java~关于开发工具和包包 Java ...
- java springboot activemq 邮件短信微服务,解决国际化服务的国内外兼容性问题,含各服务商调研情况
java springboot activemq 邮件短信微服务,解决国际化服务的国内外兼容性问题,含各服务商调研情况 邮件短信微服务 spring boot 微服务 接收json格式参数 验证参数合 ...
随机推荐
- window 命令行强制删除文件、文件夹
1. 强制删除文件文件夹和文件夹内所有文件 rd/s/q D:\app 2. 强制删除文件,文件名必须加文件后缀名 del/f/s/q D:\app.txt
- JS中的函数与对象
创建函数的三种方式 1.函数声明 function calSum1(num1, num2) { return num1 + num2; } console.log(calSum1(10, 10)); ...
- uboot使用脚本
使用mkimage命令 # mkimage -A ARM -O linux -T script -C none -n "script" -d *.sh *.img # tftp x ...
- WinServer-开关机日志
开关机日志正常1074, 6006, 13, 12, 6005,41,60081074 记录某用户在某计划下重启6006 日志服务关闭13 OS关闭时间按12 OS启动时间6005 日志服务开启 异常 ...
- 常用模块(collections模块,时间模块,random模块,os模块,sys模块,序列化模块,re模块,hashlib模块,configparser模块,logging模块)
认识模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的 ...
- [ipsec] 特别硬核的ike/ipsec NAT穿越机制分析
〇 前言 这怕是最后一篇关于IKE,IPSEC的文字了,因为不能没完没了. 所以,我一直在想这个标题该叫什么.总的来说可以将其概括为:IKE NAT穿越机制的分析. 但是,同时它也回答了以下问题: ( ...
- rest-assured-doc接口自动化测试,数据驱动测试平台
原文:https://github.com/rest-assured/rest-assured/wiki/Usage 本文github地址:https://github.com/RookieTeste ...
- 在输出debug日志前加上logger.isDebugEnabled()判断的原因
场景: String token = md5.substring(0, 10) + base64Two + md5.substring(10); if (logger.isDebugEnabled() ...
- mysql占用内存过高调优方法
最近测试一个站点,用mysql 5.6+mencache 内存16GB,但是进行查询的时候还是导致CPU占用过高,达到80%左右,所以想办法如何进行调优.以下几个参数进行参考选择 优化mysql数据 ...
- Handling skewed data---trading off precision& recall
preision与recall之间的权衡 依然是cancer prediction的例子,预测为cancer时,y=1;一般来说做为logistic regression我们是当hθ(x)>=0 ...