@AssertFalse
-
适用类型:Boolean, boolean
- 用途:验证注解的元素值是false
@AssertTrue
-
适用类型:Boolean, boolean
- 用途:验证注解的元素值是true
@DecimalMax(value=x)
-
适用类型:BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence.
- 用途:验证注解的元素值小于等于@ DecimalMax指定的value值
@DecimalMin(value=x)
-
适用类型:BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence.
- 用途:验证注解的元素值小于等于@ DecimalMin指定的value值
@Digits(integer=整数位数, fraction=小数位数)
-
适用类型:BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence.
- 用途:验证注解的元素值的整数位数和小数位数上限
@Future
-
适用类型:java.util.Date, java.util.Calendar; Additionally supported by HV, if theJoda Time date/time API is on the class path: any implementations ofReadablePartial andReadableInstant.
- 用途:验证注解的元素值(日期类型)比当前时间晚
@Max(value=x)
-
适用类型:BigDecimal, BigInteger, byte, short,int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type ofCharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number.
- 用途:验证注解的元素值小于等于@Max指定的value值
@Min(value=x)
-
适用类型:BigDecimal, BigInteger, byte, short,int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the char sequence is evaluated), any sub-type of Number.
- 用途:验证注解的元素值大于等于@Min指定的value值
@NotNull
-
适用类型:Any type
- 用途:验证注解的元素值不是null
@Null
-
适用类型:Any type
- 用途:验证注解的元素值是null
@Past
-
适用类型:java.util.Date, java.util.Calendar; Additionally supported by HV, if theJoda Time date/time API is on the class path: any implementations ofReadablePartial andReadableInstant.
- 用途:验证注解的元素值(日期类型)比当前时间早
@Pattern(regex=正则表达式, flag=)
-
适用类型:String. Additionally supported by HV: any sub-type of CharSequence.
- 用途:验证注解的元素值与指定的正则表达式匹配
@Size(min=最小值, max=最大值)
-
适用类型:String, Collection, Map and arrays. Additionally supported by HV: any sub-type of CharSequence.
- 用途:验证注解的元素值的在min和max(包含)指定区间之内,如字符长度、集合大小
@Valid
-
适用类型:Any non-primitive type(引用类型)
- 用途:验证关联的对象,如账户对象里有一个订单对象,指定验证订单对象
@NotEmpty
-
适用类型:CharSequence,Collection, Map and Arrays
- 用途:验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0)
@Range(min=最小值, max=最大值)
-
适用类型:CharSequence, Collection, Map and Arrays,BigDecimal, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types
- 用途:验证注解的元素值在最小值和最大值之间
@NotBlank
-
适用类型:CharSequence
- 用途:验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的空格
@Length(min=下限, max=上限)
-
适用类型:CharSequence
- 用途:验证注解的元素值长度在min和max区间内
-
适用类型:CharSequence
- 用途:验证注解的元素值是Email,也可以通过正则表达式和flag指定自定义的email格式
简单使用示例
标记校验注解
package com.example.demo.entity;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
public class Person {
// 标记校验name不为空
@NotBlank
private String name;
// 标记校验age大于等于18
@Min(value = 18)
private int age;
private String sex;
private float high;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public float getHigh() {
return high;
}
public void setHigh(float high) {
this.high = high;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex + ", high=" + high + "]";
}
}
启用校验注解@Valid
package com.example.demo.controller;
import java.util.List;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.entity.Person;
@Controller
public class DemoController {
/**
* 通过@Valid注解,标记启动校验。BindingResult对象获取校验结果
* @param person
* @param result
* @return
*/
@RequestMapping("/test")
@ResponseBody
public String parseJsonToEntity(@RequestBody @Valid Person person, BindingResult result) {
List<ObjectError> allErrors = result.getAllErrors();
if (result.hasErrors()) {
return JSONObject.toJSONString(allErrors);
}
return person.toString();
}
}
本文由 Administrator 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站部分文章采集自互联网,因某些原因未注明出处,如有侵权,请留言告知。