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

1、常用的注解

@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  被注释的元素必须在合适的范围内

2、

example :
vo 页面传过来的数据进行校验
inferface : 只是作为标记一个组别 可以在vo验证的某个字段上面加入多个组别,这样没有加入的组别就不会验证这个字段
controller: 需要 加入 @Validated (GroupInterface1.class) //GroupInterface1.class是定义的分组 GroupInterface2.class 需要校验的字段是不会验证的
原文链接:https://blog.csdn.net/qq920447939/article/details/80198438

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;

	

	/**** setter and getter ****/

}

3、此时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;
	}

GROUP interface(分组)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章