@Valid:标准JSR-303规范的标记型注解,用来标记验证属性和方法返回值,进行级联和递归校验
@Validated:Spring的注解,是标准JSR-303的一个变种(补充),提供了一个分组功能,可以在入参验证时,根据不同的分组采用不同的验证机制
在Controller中校验方法参数时,使用@Valid和@Validated并无特殊差异(若不需要分组校验的话)
@Validated注解可以用于类级别,用于支持Spring进行方法级别的参数校验。@Valid可以用在属性级别约束,用来表示级联校验。
@Validated只能用在类、方法和参数上,而@Valid可用于方法、字段、构造器和参数上
Validated 分组校验
@Validated(value = {DictType.add.class, DictType.update.class}) // 使用多个组进行校验
@Validated(value = DictType.add.class) //使用某一组进行校验
DictController
@PostMapping("/dict/type/{id:\\w+}")
public HttpResult updateDictTypeByTypeId(@PathVariable(required = true) String id,
@Validated(value = {DictType.add.class, DictType.update.class})
@RequestBody DictType dictType,
BindingResult bindingResult){
if (bindingResult.hasErrors()) {
System.out.println("----- 客户端的请求数据异常,所有的异常如下 ----- :");
// 取出所有的异常对象
for (FieldError fieldError : bindingResult.getFieldErrors()) {
// 打印异常的字段以及异常信息
System.out.println(fieldError.getField() + " : " + fieldError.getDefaultMessage());
return HttpResult.fail(1,fieldError.getDefaultMessage());
}
}
DictType selectDictType = dictTypeService.getDictTypeById(id);
System.out.println("----- selectDictType ----- :"+selectDictType);
if (selectDictType!=null){
dictType = dictTypeService.updateDictTypeById(id,dictType);
return HttpResult.success(dictType);
}else {
return HttpResult.fail(1,"该类型不存在");
}
}
DTO
@Data
@Builder
public class DictType {
public interface add{}
public interface update{}
@NotNull(message = "id不能为空",groups = update.class)
@Size(min=1, max=32,message = "id长度为1-32位",groups = update.class)
private String id;
@Size(min=1, max=64,message = "value长度为1-64位",groups = add.class)
private String value; // 类型值
@Size(min=1, max=64,message = "name长度为1-64位",groups = add.class)
private String name; // 类型名
@Size(min=0, max=255,message = "description长度为0-255位",groups = add.class)
private String description; // 描述
@Max(value = 11,message="sort 最大 11 值 ",groups = add.class)
// @Range(min=0,max=11,message="sort 0-11 值 ",groups = add.class)
private Integer sort; // 排序
@Size(min=0, max=32,message = "parentId长度为0-32位",groups = add.class)
private String parentId; //父Id
private String enabled; //是否可编辑
}
/*
@NotEmpty 用在集合类上面
@NotBlank 用在String上面
@NotNull 用在基本类型上
* */
单独校验
需要在controller 上加 @Validated
/**
* POST
* 根据类型id, 修改一个类型
*/
@PostMapping("/dict/type/{id:\\w+}")
public HttpResult updateDictTypeByTypeId(@NotNull(message = "id不能为空")
@Size(min=1, max=32,message = "id长度为1-32位")
@PathVariable(required = true) String id,
@NotNull(message = "name不能为空")
@Size(min=1, max=64,message = "name长度为1-64位")
@RequestParam(required = true) String name,
@NotNull(message = "value不能为空")
@Size(min=1, max=64,message = "value长度为1-64位")
@RequestParam(required = true) String value,
@NotNull(message = "sort不能为空")
@Range(min=0,max=11,message="sort长度为1-11位")
@RequestParam(required = true) Integer sort,
@Size(min=0, max=255,message = "description长度为0-255位")
@RequestParam(required = false) String description,
@Size(min=0, max=32,message = "parentId长度为0-32位")
@RequestParam(required = false) String parentId,
@RequestParam(required = false) String enabled){
DictType dictType = dictTypeService.getDictTypeById(id);
if (dictType!=null){
dictType.setName(name);
dictType.setValue(value);
dictType.setSort(sort);
dictType.setDescription(description);
dictType.setParentId(parentId);
dictType.setEnabled(enabled);
dictType = dictTypeService.updateDictTypeById(id,dictType);
return HttpResult.success(dictType);
}else {
return HttpResult.fail(1,"该类型不存在");
}
}
HttpResult
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
@Data
@JsonInclude(value = Include.NON_NULL)
public class HttpResult {
private int code;
@JsonProperty("msg")
private String message;
@JsonProperty("data")
private Object result;
public HttpResult() {
}
public HttpResult(int code, String message) {
this.code = code;
this.message = message;
}
public HttpResult(int code, Object data, String message) {
this.code = code;
this.message = message;
this.result = data;
}
public static HttpResult success() {
return new HttpResult(0, "success");
}
public static HttpResult success(Object data) {
HttpResult httpResult = success();
if(data != null) {
httpResult.setResult(data);
}
return httpResult;
}
public static HttpResult fail(int code, String message) {
return new HttpResult(code, message);
}
}
@Validated和@Valid区别:
SpringBoot整合表单验证注解@Validated,以及分组验证
SpringBoot 数据基础校验_ 分组校验 【Group Validate】
Springboot restFul 参数检验
SpringMVC参数校验
SpringBoot 表单参数校验
SpringBoot如何优雅的校验参数