首页 > 其他分享 >@Validated与@Valid的区别

@Validated与@Valid的区别

时间:2022-11-18 14:11:29浏览次数:57  
标签:String 区别 private ApiModelProperty Valid import message Validated class

  • @Validated可以配合分组使用,如QueryGroup.class,就只校验加了QueryGroup.class的属性
  • @Valid只具备基础功能,并不具备分组校验的功能
  @ApiOperation("查询动态跟踪分页")
  @PreAuthorize("@ss.hasPermi('man:manPubDynamic:list')")
  @GetMapping("/page")
  public TableDataInfo<GfManPubDynamicVo> page(@Validated(QueryGroup.class) GfManPubDynamicBo bo) {
      return iGfManPubDynamicService.queryPageList(bo);
  }
  
  
  import com.gf.common.core.validate.AddGroup;
  import com.gf.common.core.validate.EditGroup;
  import io.swagger.annotations.ApiModel;
  import io.swagger.annotations.ApiModelProperty;
  import lombok.Data;
  import lombok.EqualsAndHashCode;
  import javax.validation.constraints.*;
  import com.gf.common.core.domain.BaseEntity;
  
  /**
   *
   * @author zr
   * @date 2022-11-14
   */
  
  @Data
  @EqualsAndHashCode(callSuper = true)
  @ApiModel("动态跟踪业务对象")
  public class GfManPubDynamicBo extends BaseEntity {
  
      @ApiModelProperty(value = "动态追踪ID", required = true)
      @NotNull(message = "动态追踪ID不能为空")
      private Long dynamicId;
  
      @ApiModelProperty(value = "动态类型(0:未成年,1:低收入,2:为老,3:养老)", required = true)
      @NotNull(message = "动态类型(0:未成年,1:低收入,2:为老,3:养老)不能为空", groups = { AddGroup.class, EditGroup.class })
      private Integer dynamicType;
  
      @ApiModelProperty(value = "动态追踪主图", required = true)
      @NotBlank(message = "动态追踪主图不能为空", groups = { AddGroup.class, EditGroup.class })
      private String pics;
  
      @ApiModelProperty(value = "动态追踪内容", required = true)
      @NotBlank(message = "动态追踪内容不能为空", groups = { AddGroup.class, EditGroup.class })
      private String content;
  
      @ApiModelProperty("灯型状态")
      private Integer lightStatus;
  
      @ApiModelProperty("分页大小")
      private Integer pageSize;
  
      @ApiModelProperty("当前页数")
      private Integer pageNum;
  
      @ApiModelProperty("排序列")
      private String orderByColumn;
  
      @ApiModelProperty(value = "排序的方向", example = "asc,desc")
      private String isAsc;
  }
  • 如果@Validated校验未生效,检查自己的SpringBoot版本是否大于2.3,如果是,则需要手动增加以下依赖:
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
  • 同时,需要增加全局异常处理器,来发现异常并返回正常校验信息
/**
   * 自定义验证异常
   */
@ExceptionHandler(BindException.class)
public AjaxResult<Void> handleBindException(BindException e) {
  log.error(e.getMessage(), e);
  String message = e.getAllErrors().stream()
      .map(DefaultMessageSourceResolvable::getDefaultMessage)
      .collect(Collectors.joining(", "));
  return AjaxResult.error(message);
}

/**
   * 自定义验证异常
   */
@ExceptionHandler(ConstraintViolationException.class)
public AjaxResult<Void> constraintViolationException(ConstraintViolationException e) {
  log.error(e.getMessage(), e);
  String message = e.getConstraintViolations().stream()
      .map(ConstraintViolation::getMessage)
      .collect(Collectors.joining(", "));
  return AjaxResult.error(message);
}

/**
   * 自定义验证异常
   */
@ExceptionHandler(MethodArgumentNotValidException.class)
public AjaxResult<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
  log.error(e.getMessage(), e);
  String message = e.getBindingResult().getFieldError().getDefaultMessage();
  return AjaxResult.error(message);
}

标签:String,区别,private,ApiModelProperty,Valid,import,message,Validated,class
From: https://www.cnblogs.com/flyingrun/p/16903060.html

相关文章