@Validated分组校验及扩展
一、介绍
在springBoot
项目中,我们往往要对参数进行校验,如果在代码中进行,就会显得很杂乱冗余
我在以前有介绍过@Valid
注解的使用和扩展
但上面这篇文章整理相关的知识点有局限性,主要体现在以下方面
- 没有分组校验
- 没有嵌套校验
- 校验都是针对一个字段的,没有多个字段之间关联的校验;比如说开始日期必须小于结束日期
故此,得使用@Validated
来进行完善校验。
二、使用
1)分组校验
package com.banmoon.test.controller; | |
import com.banmoon.test.dto.ResultData; | |
import com.banmoon.test.obj.request.ValidGroupRequest; | |
import org.springframework.validation.annotation.Validated; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
/** | |
* 分组校验 | |
* | |
* @author banmoon | |
*/ | |
public class ValidController { | |
public ResultData<Void> groupSave( | |
ValidGroupRequest request) { | |
return ResultData.success(); | |
} | |
public ResultData<Void> groupUpdate( | |
ValidGroupRequest request) { | |
return ResultData.success(); | |
} | |
} |
请求类ValidGroupRequest.java
package com.banmoon.test.obj.request; | |
import io.swagger.annotations.ApiModel; | |
import io.swagger.annotations.ApiModelProperty; | |
import lombok.Data; | |
import lombok.NoArgsConstructor; | |
import javax.validation.constraints.NotBlank; | |
import javax.validation.constraints.NotNull; | |
import javax.validation.constraints.Null; | |
/** | |
* 分組校验请求-入参 | |
* | |
* @author banmoon | |
*/ | |
@Data | |
@NoArgsConstructor | |
@ApiModel("分組校验请求-入参") | |
public class ValidGroupRequest { | |
public interface DefaultGroup { | |
} | |
public interface SaveGroup { | |
} | |
public interface UpdateGroup { | |
} | |
@NotNull(message = "ID不能为空", groups = UpdateGroup.class) | |
@ApiModelProperty("ID") | |
private Integer id; | |
@NotBlank(message = "姓名不能为空", groups = DefaultGroup.class) | |
@ApiModelProperty("姓名") | |
private String name; | |
@Null(message = "性别不能修改", groups = UpdateGroup.class) | |
@NotNull(message = "性别不能为空", groups = SaveGroup.class) | |
@ApiModelProperty("性别,1=男,2=女") | |
private Integer sex; | |
} |
分别请求两个接口,且里面的都不满足
调用/valid/group/save | 调用/valid/group/update |
https://banmoon-pic.oss-cn-guangzhou.aliyuncs.com/images/20230131200930.png https://banmoon-pic.oss-cn-guangzhou.aliyuncs.com/images/20230131200935.png
在开发中,我不太喜欢这种公用一个请求类的写法。 以为这样每一个需要校验的字段,都需要加上一个group
,会写很多很麻烦 这样我还不如,复制粘贴再写一个请求类,从而达到分组的校验效果
2)嵌套校验
package com.banmoon.test.controller; | |
import com.banmoon.test.dto.ResultData; | |
import com.banmoon.test.obj.request.ValidBatchSaveRequest; | |
import org.springframework.validation.annotation.Validated; | |
import org.springframework.web.bind.annotation.*; | |
import javax.validation.Valid; | |
import javax.validation.constraints.NotEmpty; | |
import javax.validation.constraints.NotNull; | |
import java.util.List; | |
/** | |
* Valid 校验 | |
* | |
* @author banmoon | |
*/ | |
public class ValidController { | |
public ResultData<Void> batchSave( List< ValidBatchSaveRequest> request) { | |
return ResultData.success(); | |
} | |
public ResultData<Void> batchDelete( List< Integer> idList) { | |
return ResultData.success(); | |
} | |
} |
对应的请求类,ValidBatchSaveRequest.java
package com.banmoon.test.obj.request; | |
import io.swagger.annotations.ApiModel; | |
import io.swagger.annotations.ApiModelProperty; | |
import lombok.Data; | |
import lombok.NoArgsConstructor; | |
import javax.validation.constraints.NotBlank; | |
import javax.validation.constraints.NotNull; | |
/** | |
* @author banmoon | |
*/ | |
@Data | |
@NoArgsConstructor | |
@ApiModel("嵌套校验-入参") | |
public class ValidBatchSaveRequest { | |
@NotBlank(message = "姓名不能为空") | |
@ApiModelProperty("姓名") | |
private String name; | |
@NotNull(message = "性别不能为空") | |
@ApiModelProperty("性别,1=男,2=女") | |
private Integer sex; | |
} |
请求/valid/batchSave
时,两种情况
当外部的List校验失败时 | 内部的类校验失败时 |
https://banmoon-pic.oss-cn-guangzhou.aliyuncs.com/images/20230131200916.png https://banmoon-pic.oss-cn-guangzhou.aliyuncs.com/images/20230131200920.png
请求/valid/batchDelete
时,也是两种情况
当外部的List校验失败时 | 内部的包装类校验失败时 |
https://banmoon-pic.oss-cn-guangzhou.aliyuncs.com/images/20230131200903.png https://banmoon-pic.oss-cn-guangzhou.aliyuncs.com/images/20230131200907.png
如果请求类中还有其它的,需要校验的实体类,则在上面打上一个@Valid
注解即可 由于List
集合有点特殊,它需要在指定泛型的地方打上@Valid
,这样才可以对集合中的每一个对象进行校验
3)多字段联动校验
如何进行多字段之间的联动校验,就像开头讲到的那个例子一样,开始日期必须小于结束日期,
这样如何使用@Valid
进行联动校验呢,我们只需要这样
package com.banmoon.test.controller; | |
import com.banmoon.test.dto.ResultData; | |
import com.banmoon.test.obj.request.ValidFieldRelevanceRequest; | |
import org.springframework.validation.annotation.Validated; | |
import org.springframework.web.bind.annotation.PostMapping; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
import javax.validation.Valid; | |
/** | |
* Valid 校验 | |
* | |
* @author banmoon | |
*/ | |
public class ValidController { | |
public ResultData<Void> fieldRelevance( ValidFieldRelevanceRequest request) { | |
return ResultData.success(); | |
} | |
} |
重点是里面这个校验注解
package com.banmoon.test.obj.request; | |
import com.fasterxml.jackson.annotation.JsonFormat; | |
import io.swagger.annotations.ApiModel; | |
import io.swagger.annotations.ApiModelProperty; | |
import lombok.Data; | |
import lombok.NoArgsConstructor; | |
import javax.validation.constraints.AssertTrue; | |
import javax.validation.constraints.NotNull; | |
import java.util.Date; | |
/** | |
* 字段关联校验 | |
* | |
* @author banmoon | |
*/ | |
public class ValidFieldRelevanceRequest { | |
private Date beginDate; | |
private Date endDate; | |
public boolean getValid() { | |
return this.beginDate.compareTo(endDate) < 0; | |
} | |
} |
请求查看效果
三、最后
我是半月,你我一同共勉!!!