Spring Boot @Validation @Valid
Posted 盲目的拾荒者
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot @Validation @Valid相关的知识,希望对你有一定的参考价值。
@Valid是使用hibernate validation的时候使用
@Validated 是只用spring Validator 校验机制使用
基于方法参数的校验对基本类型的校验
例如:以下服务触发第一个参数的验证,确保其大小在8到10之间:
@Service
@Validated
public class MyBean {
public Archive findByCodeAndAuthor(@Size(min = 8, max = 10) String code,
Author author) {
...
}
}
基于方法参数的校验对象校验
/**
* 添加
*
*@Validated必须添加否则只在对象字段上添加不会生效
* @return ResponseEntity<Void>
*/
@PostMapping
public ResponseEntity<Void> save(@Validated @RequestBody UserDTO userDTO) {
userService.insert(userDTO);
return ResponseEntity.ok().build();
}
嵌套校验:
第一种
package org.niugang.coding.dto;
import com.sun.org.apache.bcel.internal.generic.NEW;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import javax.validation.constraints.*;
/**
* 数据传输对象
*
* @author Created by niugang on 2018/12/26/13:43
*/
@ToString
@Data
@EqualsAndHashCode
@ApiModel
public class UserDTO {
@ApiModelProperty(value = "ID", notes = "添加不同传id")
private Integer id;
/**
* {@code @NotNull javax.validation 的包 }
*/
@ApiModelProperty(value = "账号")
@NotNull
private String account;
@ApiModelProperty(value = "姓名")
@NotNull
private String name;
@NotNull
@ApiModelProperty(value = "性别")
private String sex;
@NotNull
@Pattern(regexp = "^[0-9]{11}$")
@ApiModelProperty(value = "电话")
private String phone;
@NotNull
@Email
@ApiModelProperty(value = "邮箱")
private String email;
@Valid //嵌套校验
public InnerUserDTo innerUserDTo= new InnerUserDTo();
@Data
private static class InnerUserDTo{
@NotEmpty
private String children;
}
}
第二种
package org.niugang.coding.dto;
import com.sun.org.apache.bcel.internal.generic.NEW;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import javax.validation.constraints.*;
/**
* 数据传输对象
*
* @author Created by niugang on 2018/12/26/13:43
*/
@ToString
@Data
@EqualsAndHashCode
@ApiModel
public class UserDTO {
@ApiModelProperty(value = "ID", notes = "添加不同传id")
private Integer id;
/**
* {@code @NotNull javax.validation 的包 }
*/
@ApiModelProperty(value = "账号")
@NotNull
private String account;
@ApiModelProperty(value = "姓名")
@NotNull
private String name;
@NotNull
@ApiModelProperty(value = "性别")
private String sex;
@NotNull
@Pattern(regexp = "^[0-9]{11}$")
@ApiModelProperty(value = "电话")
private String phone;
@NotNull
@Email
@ApiModelProperty(value = "邮箱")
private String email;
@Valid //嵌套校验
@NotNull
public InnerUserDTo innerUserDTo;
@Data
private static class InnerUserDTo{
@NotEmpty
private String children;
}
}
微信公众号
JAVA程序猿成长之路
分享资源,记录程序猿成长点滴。专注于Java,Spring,SpringBoot,SpringCloud,分布式,微服务。
以上是关于Spring Boot @Validation @Valid的主要内容,如果未能解决你的问题,请参考以下文章
Spring-Boot 如何正确注入 javax.validation.Validator
Spring Boot 中的 javax.validation.constraints.Email
spring boot:validation+aop 注解方式实现校验
Spring Boot应用使用Validation校验入参,现有注解不满足,我是怎么暴力扩展validation注解的
spring boot 表单的实体提交错误:Validation failed for object='book'. Error count: 2