SpringMVC——BindingResult验证框架Validation

public ResponseResult PotentialProductStatusChange(@RequestBody @Valid ProductStatusChangeForm productStatusChangeForm, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            return new ResponseResult(ResponseCode.IS_FAILURE,allErrors.get(0).getDefaultMessage());
        }
        return responseResult;
    }

  方法的第二个参数紧跟在验证的JavaBean后面,

    @Valid是验证框架的注解,验证的JavaBean:

public class ProductStatusChangeForm {

    //产品ID列表
    @Size(min = 1,message = "记录数为0没有意义")
    private List<Integer> masterIndexList;
    //设置状态类型1-提交;2-审核
    @NotNull(message = "类型不能为空")
    private Integer  type;
}

  @Valid属于SpringMVC的注解,@Validated属于hibernate的注解

posted @ 2018-05-03 21:31  旧雨衣  阅读(1033)  评论(0编辑  收藏  举报