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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

长沙郭富城   2021-04-22 我要评论

本文介绍了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<String, Object> validate(
     @Valid @RequestBody ValidatorPojo vp, Errors errors) {
   Map<String, Object> errMap = new HashMap<>();
   // 获取错误列表
   List<ObjectError> 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 注解验证 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持! 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们