1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-validation</artifactId>
  4. </dependency>

控制器

@Validated 控制器上打@Validated注解
@Valid 实体对象上打@Valid注解

  1. @Validated
  2. @Api(tags = "用户配置中心接口")
  3. @RestController
  4. @RequestMapping("/ucc")
  5. public class UccEntityController {
  6. @ApiOperation("根据key更新配置信息")
  7. @PutMapping("/{key}")
  8. public ResultVO updateUccByKey(@ApiParam(value = "主键key", defaultValue = "ALL_CWA6920")
  9. @PathVariable("key") String key,
  10. @RequestBody @Valid UpdateDTO dto) {
  11. return ResultVO.success(uccEntityService.updateUccByKey(key, dto));
  12. }
  13. }

实体对象

  1. public class UpdateDTO {
  2. @NotBlank(message = "唯一键不能为空")
  3. @ApiModelProperty(value = "唯一key", example = "ALL_CWA6920")
  4. private String key;
  5. }