当前位置:网站首页>【无标题】
【无标题】
2022-07-01 22:54:00 【loliDapao】
想要使用validated需要引入Jar包,有两个方式,选择一种即可:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.5.Final</version>
</dependency>
必须要引入,否则@validated不会生效
普通模式(默认是这个模式):会校验完所有的属性,然后返回所有的验证失败信息。
快速失败模式:只要有一个验证失败,则返回。
通常在实际开发中,我们需要配置快速失败模式,快速失败模式的配置方式:
package com.morris.validator.config;
import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;@Configuration
public class ValidatorConfig {@Bean
public Validator validator() {
ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class)
.configure()
// 快速失败模式
.failFast(true)
.buildValidatorFactory();
return validatorFactory.getValidator();
}
}
@Bean
public Validator validator() {
ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class)
.configure()
// 快速失败模式
//.failFast(true)
.addProperty( "hibernate.validator.fail_fast", "true" )
.buildValidatorFactory();
return validatorFactory.getValidator();
}
常用注解说明
@AssertFalse:
所注解的元素必须是Boolean类型,并且值为false
@AssertTrue:
所注解的元素必须是Boolean类型,并且值为true
@DecimalMax:
所注解的元素必须是数字,并且值要小于或等于给定的BigDecimalString值
@DecimalMin:
所注解的元素必须是数字,并且值要小于或等于给定的BigDecimalString值
@Digits:
所注解的元素必须是数字,并且它的值必须有指定的位数
@Email:
所注解的元素要匹配指定的正则表达式
@Max:
所注解的元素必须是数字,并且值要小于或等于给定的值。注意如果@Max所注解的元素是null,则@Max注解
会返回true,所以应该把@Max注解和@NotNull注解结合使用
@Min:
所注解的元素必须是数字,并且值要大于或等于给定的值。注意如果@Min所注解的元素是null,则@Min注解
会返回true,即也会通过校验,所以应该把@Min注解和@NotNull注解结合使用。
@NotBlank:
所注解的元素不能为null且不能为空白,用于校验CharSequence(含String、StringBuilder和StringBuffer)
@NotEmpty:
所注解的元素不能为null且长度大于0,可以是空白,用于校验CharSequence、数组、Collection和Map
@NotNull:
所注解的元素不能为null
@Null:
所注解的元素必须为null
@Pattern:
所注解的元素必须匹配指定的正则表达式。注意如果@Pattern所注解的元素是null,则@Pattern注解会返回
true,即也会通过校验,所以应该把@Pattern注解和@NotNull注解结合使用
@Size:
所注解的元素必须符合指定的大小,该注解可用于数组,CharSequence(含String、StringBuilder和
StringBuffer),Collection和Map。注意如果@Size所注解的元素是null,则@Size注解会返回true,即也
会通过校验,所以应该把@Size注解和@NotNull注解结合使用
eg:
@Data
public class Address {
@NotBlank(message = "province不能为空")
private String province;
private String city;
}
import com.example.demovalidator.Vo.ResultVo;
import com.example.demovalidator.enums.ErrorCode;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;
import java.util.stream.Collectors;
@ControllerAdvice
@ResponseBody
public class CtrIAdvice {
@PostMapping("/test")
public ResultVo testValited(@Validated @RequestBody Address address){
return ResultVo.fail("失败了");
}
}边栏推荐
- SWT / anr problem - SWT causes kernel fuse deadlock
- Paramètres communs de matplotlib
- 2022年起重机司机(限桥式起重机)考试试题及模拟考试
- 问题随记 —— file /usr/share/mysql/charsets/README from install of MySQL-server-5.1.73-1.glibc23.x86_64 c
- dat.GUI
- 每日三题 6.28
- Jerry's burning of upper version materials requires [chapter]
- MySQL binlog cleanup
- Istio、eBPF 和 RSocket Broker:深入研究服务网格
- Matplotlib常用设置
猜你喜欢

硅谷产品实战学习感触
![[MySQL] basic use of explain and the function of each column](/img/d6/64f65ba21f5cda2c409477705f6a79.png)
[MySQL] basic use of explain and the function of each column

Huisheng Huiying 2022 intelligent, fast and simple video editing software

CKS CKA ckad change terminal to remote desktop

众昂矿业:发展以氟化工为主的特色化工产业具有先天优势

物联网现状及未来发展趋势

物联网开发零基础教程

建模和影视后期有什么关联?

神经网络物联网的发展趋势和未来方向

Commemorate becoming the first dayus200 tripartite demo contributor
随机推荐
Matplotlib常用设置
建模和影视后期有什么关联?
问题随记 —— /usr/bin/perl is needed by MySQL-server-5.1.73-1.glibc23.x86_64
dat.GUI
mt管理器测试滑雪大冒险
Yoga27 multidimensional all-in-one computer with excellent appearance and high-end configuration
Behind sharing e-commerce: the spirit of CO creation, symbiosis, sharing, CO prosperity and win-win
【微服务|Sentinel】sentinel整合openfeign
CKS CKA ckad change terminal to remote desktop
Some abilities can't be learned from work. Look at this article, more than 90% of peers
Is it safe to choose mobile phone for stock trading account opening in Shanghai?
The online beggar function of Japanese shopping websites
dat. GUI
云信小课堂 | IM及音视频中常见的认知误区
win 10 mstsc连接 RemoteApp
CADD课程学习(3)-- 靶点药物相互作用
众昂矿业:发展以氟化工为主的特色化工产业具有先天优势
神经网络物联网的发展趋势和未来方向
2022 safety officer-c certificate examination question simulation examination question bank and simulation examination
Openresty load balancing