Python 官方文档:入门教程 => 点击学习
目录jsR303介绍引入依赖常用注解开启校验数据校验测试自定义的封装错误信息统一异常处理分组校验创建分组校验接口添加校验注解开启分组校验自定义校验编写自定义的校验注解编写自定义的校验
在Java中提供了一系列的校验方式
这些校验方式在javax.validation.constraints
包中
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
@Null 验证对象是否为null
@NotNull 验证对象是否不为null, 无法查检长度为0的字符串
@NotBlank 检查约束字符串是不是Null还有被Trim的长度是否大于0,只对字符串,且会去掉前后空格.
@NotEmpty 检查约束元素是否为NULL或者是EMPTY.
Booelan检查
长度检查
日期检查
数值检查
建议使用在Stirng,Integer类型,不建议使用在int类型上,因为表单值为“”时无法转换为int,但可以转换为Stirng为”“,Integer为null
controller中加校验注解@Valid,开启校验
步骤1:实体类字段上使用校验注解 @NotNull @NotEmpty @NotBlank @Pattern
步骤2:controller中加校验注解@Valid,开启校验
步骤3:给校验的Bean后,紧跟一个BindingResult,就可以获取到校验的结果
public R save(@Valid @RequestBody User user, BindingResult result){}
实体中添加注解
@Data
public class Student {
@NotEmpty(message ="姓名不能为空")
private String name;
}
controller层中保存方法添加:@Valid
@PostMapping("/jsr")
public ajaxResult testJrs(@Valid @RequestBody User user, BindingResult result) {
String name = user.getName();
HashMap<String, Object> map = new HashMap<>();
map.put("name", name);
map.put("errors", result.getFieldErrors());
return AjaxResult.success("数据校验", map);
}
数据校验测试:测试:Http://localhost:8080/test/jsr
@Data
public class User {
@NotEmpty(message = "姓名不能为空")
@apiModelProperty("姓名")
private String name;
@ApiModelProperty("学号")
private String id;
@ApiModelProperty("年龄")
private String age;
}
返回信息
{
"msg": "数据校验",
"code": 200,
"data": {
"name": "",
"errors": [
{
"codes": [
"NotEmpty.user.name",
"NotEmpty.name",
"NotEmpty.java.lang.String",
"NotEmpty"
],
"arguments": [
{
"codes": [
"user.name",
"name"
],
"defaultMessage": "name",
"code": "name"
}
],
"defaultMessage": "姓名不能为空",
"objectName": "user",
"field": "name",
"rejectedValue": "",
"bindingFailure": false,
"code": "NotEmpty"
}
]
}
}
@PostMapping("/package")
public AjaxResult testPackage(@Valid @RequestBody User user, BindingResult result) {
String name = user.getName();
Map<String, String> map = new HashMap<>();
map.put("name", name);
if (result.hasErrors()) {
//1.获取错误的校验结果
result.getFieldErrors().forEach((item) -> {
//2.获取发生错误时的message
String message = item.getDefaultMessage();
//3.获取发生错误的字段
String field = item.getField();
map.put(field, message);
});
return AjaxResult.error("数据校验", map);
} else {
return AjaxResult.success(map);
}
}
自定义的封装错误信息:测试:http://localhost:80/test/package
{
"name": "",
"id": "demoData",
"age": "demoData"
}
错误信息
{
"msg": "数据校验",
"code": 500,
"data": {
"name": "姓名不能为空"
}
}
@Slf4j
@RestControllerAdvice(basePackages = "com.michale.jrs303.controllers")
public class FireflyMallExceptionControllerAdvice {
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public Result handleVaildException(MethodArgumentNotValidException e) {
log.error("数据校验出现问题:{},异常类型:{}", e.getMessage(), e.getClass());
BindingResult bindingResult = e.getBindingResult();
Map<String, String> errORMap = new HashMap();
bindingResult.getFieldErrors().forEach((fieldError) -> {
errorMap.put(fieldError.getField(), fieldError.getDefaultMessage());
});
return Result.fail(errorMap, "数据校验出现问题");
}
@ExceptionHandler(value = Throwable.class)
public Result handleException(Throwable throwable) {
return Result.fail();
}
}
@RequestMapping("/testException")
public Result testException(@Valid @RequestBody Student student) {
String name = student.getName();
Map<String, String> map = new HashMap<>();
map.put("name", name);
return Result.ok(map);
}
测试统一异常处理:测试:http://localhost:8080/testException
{
"msg": "数据校验出现问题",
"path": "/test/testException",
"code": 414,
"errors": {
"name": "姓名不能为空"
}
}
错误信息
{
"code": 500,
"msg": "数据校验出现问题",
"data": {
"name": "姓名不能为空"
}
}
public interface NameGroup {
}
public interface AgeGroup {
}
@Data
public class Student {
@NotEmpty(message ="姓名不能为空",groups = NameGroup.class)
private String name;
@NotEmpty(message ="绰号不能为空",groups = NameGroup.class)
private String nickName;
@Min(value = 18,message = "年龄下限不能低于18岁" ,groups = AgeGroup.class)
private String age;
@Max(value = 60,message = "年龄上限不能超过60岁" ,groups = AgeGroup.class)
private String retireAge;
}
@Validated(NameGroup.class)指定校验分组
@RequestMapping("/testGroup")
public Result testGroup(@Validated(NameGroup.class) @RequestBody Student student) {
String name = student.getName();
String nickName = student.getNickName();
String age = student.getAge();
String retireAge = student.getRetireAge();
Map<String, String> map = new HashMap<>();
map.put("name", name);
map.put("nickname", nickName);
map.put("age", age);
map.put("retireAge", retireAge);
return Result.ok(map);
}
测试分组校验:http://localhost:8080/testGroup
{
"name":"",
"nickName":"",
"age":"17",
"retireAge":"66"
}
错误信息
{
"code": 500,
"msg": "数据校验出现问题",
"data": {
"nickName": "绰号不能为空",
"name": "姓名不能为空"
}
}
@Validated(AgeGroup.class)指定校验分组
@RequestMapping("/testGroup")
public Result testGroup(@Validated(AgeGroup.class) @RequestBody Student student) {
String name = student.getName();
String nickName = student.getNickName();
String age = student.getAge();
String retireAge = student.getRetireAge();
Map<String, String> map = new HashMap<>();
map.put("name", name);
map.put("nickname", nickName);
map.put("age", age);
map.put("retireAge", retireAge);
return Result.ok(map);
}
测试分组校验:http://localhost:8080/testGroup
{
"name":"",
"nickName":"",
"age":"17",
"retireAge":66
}
错误信息
{
"code": 500,
"msg": "数据校验出现问题",
"data": {
"retireAge": "年龄上限不能超过60岁",
"age": "年龄下限不能低于18岁"
}
}
比如要创建一个:@ListValue 注解,被标注的字段值只能是:0或1
@Documented
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
public @interface ListValue {
// 使用该属性去Validation.properties中取
String message() default "{com.atguigu.common.valid.ListValue.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
int[] value() default {};
}
设置错误信息:创建文件ValidationMessages.properties
com.firefly.common.valid.ListValue.message=必须提交指定的值 [0,1]
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;
public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {
private Set<Integer> set = new HashSet<>();
@Override
public void initialize(ListValue constraintAnnotation) {
//获取注解允许的值
int[] value = constraintAnnotation.value();
for (int i : value) {
set.add(i);
}
}
@Override
public boolean isValid(Integer value, ConstraintValidatorContext context) {
//判断传入的值是否在满足允许的值
boolean b = set.contains(value);
return b;
}
}
在@ListValue注解关联校验器
@Constraint(validatedBy = { ListValueConstraintValidator.class})
一个校验注解可以匹配多个校验器
@ListValue(value = {0,1},groups = {AgeGroup.class,MyJRS303Group.class})
private Integer gender;
测试自定义校验器:http://localhost:8080/testGroup
{
"gender":"3"
}
{
"code": 500,
"msg": "数据校验出现问题",
"data": {
"gender": "必须提交指定的值 [0,1]"
}
}
以上就是Java利用自定义注解实现数据校验的详细内容,更多关于Java数据校验的资料请关注编程网其它相关文章!
--结束END--
本文标题: Java利用自定义注解实现数据校验
本文链接: https://lsjlt.com/news/166704.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0