JSR是Java Specification Requests的缩写,意思是Java 规范提案。JSR-303 是JAVA EE 6 中的一项子规范,叫做Bean Validation,Hibernate Validator 是 Bean Validation 的参考实现 . Hibernate Validator 提供了 JSR 303 规范中所有内置 constraint 的实现,除此之外还有一些附加constraint。
Springboot中可以用@validated来校验数据,如果数据异常则会统一抛出异常,方便异常中心统一处理。
@Null 验证对象是否为null
@NotNull 验证对象是否不为null, 无法查检长度为0的字符串
@NotBlank 检查约束字符串是不是Null还有被Trim的长度是否大于0,只对字符串,且会去掉前后空格.
@NotEmpty 检查约束元素是否为NULL或者是EMPTY.
@AssertTrue 验证 Boolean 对象是否为 true
@AssertFalse 验证 Boolean 对象是否为 false
@Size(min=, max=) 验证对象(Array,Collection,Map,String)长度是否在给定的范围之内
@Length(min=, max=) Validates that the annotated string is between min and max included.
@Past 验证 Date 和 Calendar 对象是否在当前时间之前,验证成立的话被注释的元素一定是一个过去的日期
@Future 验证 Date 和 Calendar 对象是否在当前时间之后 ,验证成立的话被注释的元素一定是一个将来的日期
@Pattern 验证 String 对象是否符合正则表达式的规则,被注释的元素符合制定的正则表达式,
regexp:正则表达式 flags: 指定 Pattern.Flag 的数组,表示正则表达式的相关选项。
建议使用在Stirng,Integer类型,不建议使用在int类型上,因为表单值为“”时无法转换为int,但可以转换为Stirng为”“,Integer为null
@Min 验证 Number 和 String 对象是否大等于指定的值
@Max 验证 Number 和 String 对象是否小等于指定的值
@DecimalMax 被标注的值必须不大于约束中指定的最大值. 这个约束的参数是一个通过BigDecimal定义的最大值的字符串表示.小数存在精度
@DecimalMin 被标注的值必须不小于约束中指定的最小值. 这个约束的参数是一个通过BigDecimal定义的最小值的字符串表示.小数存在精度
@Digits 验证 Number 和 String 的构成是否合法
@Digits(integer=,fraction=) 验证字符串是否是符合指定格式的数字,interger指定整数精度,fraction指定小数精度。
@Range(min=, max=) 被指定的元素必须在合适的范围内
@Range(min=10000,max=50000,message=”range.bean.wage”)
@Valid 递归的对关联对象进行校验, 如果关联对象是个集合或者数组,那么对其中的元素进行递归校验,如果是一个map,则对其中的值部分进行校验.(是否进行递归验证)
@CreditCardNumber信用卡验证
@Email 验证是否是邮件地址,如果为null,不进行验证,算通过验证。
@ScriptAssert(lang= ,script=, alias=)
@URL(protocol=,host=, port=,regexp=, flags=)
@NotBlank 检查约束字符串是不是Null还有被Trim的长度是否大于0,只对字符串,且会去掉前后空格.
@NotEmpty 检查约束元素是否为NULL或者是EMPTY.
@Length 被检查的字符串长度是否在指定的范围内
@CreditCardNumber信用卡验证 @Email 验证是否是邮件地址,如果为null,不进行验证,算通过验证。
@URL 验证是否是一个url地址
添加maven依赖
javax.validation
validation-api
在参数上加上校验注解,如果参数是自定义类型,则在类的属性上加校验注解。
使校验注解生效
2.1 直接在参数上加校验注解,需要在类上加@Validated
2.1 自定义类型,变量前面加@Validated或者@Valid
@Data
public class Demo {
//不能为空且不能为空串
@Null(message = "姓名不能为空")
private String username;
}
@PostMapping("/demo/01")
public Result demo1(@Valid Dem demo,@NotBlank String email){
return Result.success(200,"成功");
}
@Validated和@Valid的区别:
@Validated:
Spring提供的
支持分组校验
可以用在类型、方法和方法参数上。但是不能用在成员属性(字段)上
由于无法加在成员属性(字段)上,所以无法单独完成级联校验,需要配合@Valid
@Valid:
JDK提供的(标准JSR-303规范)
不支持分组校验
可以用在方法、构造函数、方法参数和成员属性(字段)上
可以加在成员属性(字段)上,能够独自完成级联校验
@Data
public class Emp implements Serializable {
//不能为空且不能为空串(调用trim()后)
@NotBlank(message = "账号不能为空")
private String username;
@Valid //需要加上,否则不会验证Dept类中的校验注解
@NotNull //并且需要触发该字段的验证才会进行嵌套验证。
private Dept dept;
}
@Data
public class Dept implements Serializable {
@NotBlank(message = "deptNameb不能为空")
private String deptName;
}
定义接口
public interface IGroup {
interface Registry extends Default {}
interface Update extends Default {}
}
指定校验的组
@Data
public class Emp implements Serializable {
//当校验的组为update时才校验该字段
@NotNull(message = "编号不能为空",groups = {IGroup.Update.class})
@Min(value = 1,groups = {IGroup.Update.class})
private Integer empNo;
//不能为空且不能为空串(调用trim()后)
@NotBlank(message = "账号不能为空")
private String username;
@Pattern(regexp = "^[0-9A-z]{10,18}$",message = "密码只能使用数字+字母",groups = IGroup.Registry.class)
private String password;
@Valid
@NotNull
private Dept dept;
}
@PostMapping("/emp/add") //指定需要校验的组
public Result addEmp(@RequestBody @Validated(IGroup.Registry.class) Emp emp){
return Result.success(200,"成功");
}
指定组与组之间的检验顺序,如果第一个组校验没过,就不会校验后面的组
@GroupSequence({Default.class,IGroup.Update.class, IGroup.Registry.class})
public interface IGroup {
interface Registry extends Default {}
interface Update extends Default {}
}
@PostMapping("/emp/add")
public Result addEmp(@RequestBody @Validated({IGroup.class}) Emp emp){
return Result.success(200,"成功");
}
(1)、定义模型
public interface CaseMode{
String UPPER="大写";
String LOWER="小写";
}
(2)、创建自定义注解
@Target( FIELD, METHOD, PARAMETER, ANNOTATION_TYPE, TYPE_USE })
@Retention(RUNTIME)
@Constraint(validatedBy = CheckCaseValidator.class) //指定自定义验证器
@Documented
@Repeatable(CheckCase.List.class) //表示可以在同一位置重复多次
public @interface CheckCase {
//默认的错误信息
String message() default "{verification.default.Errormessage}";
Class>[] groups() default { };
Class extends Payload>[] payload() default { };
String value();
@Target({ FIELD, METHOD, PARAMETER, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Documented
@interface List {
CheckCase[] value();
}
}
(3)、创建自定义验证器第一个泛型是自定义注解、第二个是校验值的类型,也就是注解标注的字段的类型
public class CheckCaseValidator implements ConstraintValidator {
private String caseMode;
@Override
public void initialize(CheckCase constraintAnnotation) {
this.caseMode = constraintAnnotation.value();
}
/**
* 判断是否通过校验
* @param value 传入的值
* @param context
* @return
*/
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if ( value == null ) {
return true;
}
if (CaseMode.UPPER.equals(caseMode) ) {
return value.equals( value.toUpperCase() );
}
else {
return value.equals( value.toLowerCase() );
}
}
}
(4)、在 resources 目录下创建一个 ValidationMessages.properties 配置文件,key 是第二步 message 设置的默认值,value 是自定义错误信息。{value}为 @CheckCase的value属性的值
verification.default.Errormessage=字母必须为全为{value}
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(BindException.class)
public HashMap bindExceptionHandler(BindException e){
HashMap map = new HashMap<>();
e.getBindingResult().getFieldErrors().forEach(field -> {
map.put(field.getField(), field.getDefaultMessage());
});
return map;
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public HashMap methodArgumentNotValidException(MethodArgumentNotValidException e){
HashMap map = new HashMap<>();
e.getBindingResult().getFieldErrors().forEach(field -> {
map.put(field.getField(), field.getDefaultMessage());
});
return map;
}
@ExceptionHandler(ConstraintViolationException.class)
public HashMap handle(ConstraintViolationException e) {
HashMap map = new HashMap<>();
e.getConstraintViolations().forEach(item->{
map.put(item.getPropertyPath().toString(),item.getMessage());
});
return map;
}
}
@PostMapping("/emp/test")
public Result test(@Validated Emp emp, BindingResult validResult){
if (validResult.hasErrors()){
HashMap map = new HashMap<>();
validResult.getFieldErrors().forEach(error->{
map.put(error.getField(),error.getDefaultMessage());
});
return Result.error(HttpStatus.BAD_REQUEST.value(),map);
}
return Result.success(HttpStatus.OK.value(),"成功");
}
@SpringBootTest
public class ValidatorTest {
private static Validator validator = Validation.byProvider(HibernateValidator.class)
.configure()
.failFast(false) // 是否开启快速失败模式
.buildValidatorFactory()
.getValidator();
@Test
public void test1(){
Emp emp = new Emp();
//单独校验某个属性
//Set> validProperty = validator.validateProperty(emp, "username");
//检验对象
Set> validBean = validator.validate(emp);
Iterator> iterator = validBean.iterator();
while (iterator.hasNext()){
ConstraintViolation next = iterator.next();
String property = next.getPropertyPath().toString();
String message = next.getMessage();
System.out.println(property+":"+message);
}
}
}
在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml , 用来指定多个环境版本。例如:
application-test.properties 代表测试环境配置
application-dev.properties 代表开发环境配置
如果yml和properties同时都配置了端口,并且没有激活其他环境 , 默认会使用properties配置文件的!
配置文件可以存放在:
项目根目录下的config目录下
项目根目录
classpath(resources)下的config目录下
classpath下
当配置文件名称相同时,优先级为:
项目目录下的config > 项目的根目录 > classpath下的config目录 > classpath
properties.yaml配置:
(1)、分别创建对应的配置文件
application-环境名.properties|.yml
application-pro.properties |.yml正式
appication-dev.properties|.yml开发
application-test.properties|.yml 测试
(2)、选择某一具体环境,在主配置中添加:
properties:
spring.profies.active=环境名yml方式:
spring:
profiles:
active: pro
如果将application.properties文件注释掉,sprinboot仍然会读取其他的application-环境名.properties中的配置;且properties文件的优先级高于yml文件
#第一个环境(主环境)
server:
port: 9999
spring:
profiles:
active: dev #指定使用哪个环境
--- #使用 --- 来作为分隔符
#第二个环境(开发环境)
server:
port: 8001
spring:
profiles: dev
---
#第三个环境(测试环境)
server:
port: 8002
spring:
profiles: test
---
在maven中使用profiles标签;
dev
true
dev
8082
8085
5583
8888
127.0.0.1
9908
9909
9910
test
false
test
8083
pro
false
pro
8084
分别创建对应的配置文件
application-环境名.properties|.yml
application-pro.properties |.yml正式
application-dev.properties|.yml开发
application-test.properties|.yml 测试
在主配置文件中使用@spring.profiles.active@来引入;
[email protected]@