spring注解验证@NotNull等使⽤⽅法
本⽂介绍了spring 注解验证@NotNull等使⽤⽅法,分享给⼤家,具体如下:
常⽤标签
@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) 被注释的元素必须符合指定的正则表达式。
@Email 被注释的元素必须是电⼦邮件地址
@Length 被注释的字符串的⼤⼩必须在指定的范围内
@NotEmpty  被注释的字符串必须⾮空
@Range  被注释的元素必须在合适的范围内
example :
vo 页⾯传过来的数据进⾏校验
inferface : 只是作为标记⼀个组别可以在vo验证的某个字段上⾯加⼊多个组别,这样没有加⼊的组别就不会验证这个字段controller: 需要加⼊@Validated (GroupInterface1.class) //GroupInterface1.class是定义的分组 GroupInterface2.class 需要校验的字段是不会验证的
VO:
public class User implements Serializable {
/**
* 主键
*/
@NotNull(message = "primary is not null",groups = {GroupInterface1.class})
private Long id;
@Pattern(regexp = "[0123456789]",groups = {GroupInterface1.class,GroupInterface2.class},message = "hava a error Date")
private Long maxDiscountAmount;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime;
@Future(message = "expireTime is not less than now",groups = {GroupInterface1.class,GroupInterface2.class})
@NotNull(message = "expireTime is not null",groups = {GroupInterface1.class,GroupInterface2.class})
private Date expireTime;
}
另外⼀个例⼦:
import java.util.Date;
import straints.DecimalMax;
import straints.DecimalMin;
import straints.Email;
import straints.Future;
import straints.Max;
import straints.Min;
import straints.NotNull;
import straints.Size;
import org.straints.Range;
import org.springframework.format.annotation.DateTimeFormat;
/**** imports ****/
public class ValidatorPojo {
/
/ ⾮空判断
@NotNull(message = "id不能为空")
private Long id;
@Future(message = "需要⼀个将来⽇期") // 只能是将来的⽇期
// @Past //只能去过去的⽇期
@DateTimeFormat(pattern = "yyyy-MM-dd") // ⽇期格式化转换
@NotNull // 不能为空
private Date date;
@NotNull // 不能为空
@DecimalMin(value = "0.1") // 最⼩值0.1元
@DecimalMax(value = "10000.00") // 最⼤值10000元
private Double doubleValue = null;
@Min(value = 1, message = "最⼩值为1") // 最⼩值为1
@Max(value = 88, message = "最⼤值为88") // 最⼤值88
@NotNull // 不能为空
private Integer integer;
@Range(min = 1, max = 888, message = "范围为1⾄888") // 限定范围 private Long range;
// 邮箱验证
@Email(message = "邮箱格式错误")
private String email;
@Size(min = 20, max = 30, message = "字符串长度要求20到30之间。") private String size;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Double getDoubleValue() {
return doubleValue;
}
public void setDoubleValue(Double doubleValue) {
this.doubleValue = doubleValue;
}
public Integer getInteger() {
return integer;
}
public void setInteger(Integer integer) {
this.integer = integer;
}
public Long getRange() {
return range;
}
public void setRange(Long range) {
this.range = range;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
}
public String getSize() {
springmvc常用标签
return size;
}
public void setSize(String size) {
this.size = size;
}
/**** setter and getter ****/
}
此时controller应该要加上@Valid ,否则不会验证!
/
***
* 解析验证参数错误
* @param vp —— 需要验证的POJO,使⽤注解@Valid 表⽰验证
* @param errors 错误信息,它由Spring MVC通过验证POJO后⾃动填充
* @return 错误信息Map
*/
@RequestMapping(value = "/valid/validate")
@ResponseBody
public Map<String, Object> validate(
@Valid @RequestBody ValidatorPojo vp, Errors errors) {
Map<String, Object> errMap = new HashMap<>();
/
/ 获取错误列表
List<ObjectError> oes = AllErrors();
for (ObjectError oe : oes) {
String key = null;
String msg = null;
// 字段错误
if (oe instanceof FieldError) {
FieldError fe = (FieldError) oe;
key = fe.getField();// 获取错误验证字段名
} else {
// ⾮字段错误
key = oe.getObjectName();// 获取验证对象名称
}
// 错误信息
msg = oe.getDefaultMessage();
errMap.put(key, msg);
}
return errMap;
}
到此这篇关于spring 注解验证@NotNull等使⽤⽅法的⽂章就介绍到这了,更多相关spring 注解验证内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!