统一验证
S2B2C 白洞版 · 统一验证
示例值注意事项:本文中的 IP、域名、账号、密码等仅为示例,请按你的实际环境替换。
- 适用于二开技术团队在对应模块下的开发、调试或配置工作。
- 已拉取对应项目源码并具备基础运行环境。
- 已根据实际环境替换文中的示例配置值。
本商城使用spring提供的统一校验工具spring-boot-starter-validation对请求进行校验
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId></dependency>这里通过注解封装了几种常用的校验
@NotNull不能为null@NotEmpty不能为null、空字符串、空集合@NotBlank不能为null、空字符串、纯空格的字符串@Min数字最小值不能小于x@Max数字最大值不能大于x@Email字符串为邮件格式@Max数字最大值不能大于x@Size字符串长度最小为x、集合长度最小为x@Pattern正则表达式
我们以SpuDTO为例,看看怎么使用
public class SpuDTO{ private static final long serialVersionUID = 1L;
@ApiModelProperty("spuId") private Long spuId;
@ApiModelProperty("品牌ID") private Long brandId;
@NotNull(message = "分类不能为空") @ApiModelProperty("分类ID") private Long categoryId;
@NotNull(message = "店铺分类不能为空") @ApiModelProperty("店铺分类ID") private Long shopCategoryId;
@NotNull(message = "商品名称不能为空") @ApiModelProperty("spu名称") private String name;
/** 省略其余字段以及get、set、tostring方法*/}我们在Controller层使用该bean,并使用@Valid注解,使校验的注解生效,如SpuController :
@RestController("platformSpuController")@RequestMapping("/admin/spu")@Api(tags = "admin-spu信息")public class SpuController {
@Autowired private SpuService spuService;
@PostMapping @ApiOperation(value = "保存spu信息", notes = "保存spu信息") public ServerResponseEntity<Void> save(@Valid @RequestBody SpuDTO spuDTO) { checkSaveOrUpdateInfo(spuDTO); spuService.save(spuDTO); return ServerResponseEntity.success(); }}并且在DefaultExceptionHandlerConfig 拦截由@Valid 触发的异常信息并返回:
@RestController@RestControllerAdvicepublic class DefaultExceptionHandlerConfig {
@ExceptionHandler({ MethodArgumentNotValidException.class, BindException.class }) public ResponseEntity<ServerResponseEntity<List<String>>> methodArgumentNotValidExceptionHandler(Exception e) { logger.error("methodArgumentNotValidExceptionHandler", e); List<FieldError> fieldErrors = null; if (e instanceof MethodArgumentNotValidException) { fieldErrors = ((MethodArgumentNotValidException) e).getBindingResult().getFieldErrors(); } if (e instanceof BindException) { fieldErrors = ((BindException) e).getBindingResult().getFieldErrors(); } if (fieldErrors == null) { return ResponseEntity.status(HttpStatus.OK) .body(ServerResponseEntity.fail(ResponseEnum.METHOD_ARGUMENT_NOT_VALID)); } List<String> defaultMessages = new ArrayList<>(fieldErrors.size()); for (FieldError fieldError : fieldErrors) { defaultMessages.add(fieldError.getField() + ":" + fieldError.getDefaultMessage()); } return ResponseEntity.status(HttpStatus.OK) .body(ServerResponseEntity.fail(ResponseEnum.METHOD_ARGUMENT_NOT_VALID, defaultMessages)); }
@ExceptionHandler({ HttpMessageNotReadableException.class }) public ResponseEntity<ServerResponseEntity<List<FieldError>>> methodArgumentNotValidExceptionHandler( HttpMessageNotReadableException e) { logger.error("methodArgumentNotValidExceptionHandler", e); return ResponseEntity.status(HttpStatus.OK) .body(ServerResponseEntity.fail(ResponseEnum.HTTP_MESSAGE_NOT_READABLE)); }}- 根据文中步骤完成后,确认对应功能可访问或服务可正常启动。
- 可先查看故障排查索引:
../../04-故障排查/README.md
- 当前目录索引:
README.md