logo资料库

spring 注解验证@NotNull等使用方法.pdf

第1页 / 共3页
第2页 / 共3页
第3页 / 共3页
资料共3页,全文预览结束
注解验证@NotNull等使用方法 等使用方法 spring 注解验证 主要介绍了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 javax.validation.constraints.DecimalMax; import javax.validation.constraints.DecimalMin; import javax.validation.constraints.Email; import javax.validation.constraints.Future; import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import org.hibernate.validator.constraints.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) { this.email = email; } public String getSize() { 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 validate( @Valid @RequestBody ValidatorPojo vp, Errors errors) { Map errMap = new HashMap<>(); // 获取错误列表 List oes = errors.getAllErrors(); 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 注解验证 内容请搜索我们以前的文章 或继续浏览下面的相关文章希望大家以后多多支持我们!
分享到:
收藏