美文网首页我爱编程
SpringBoot使用JSR-303对参数进行校验

SpringBoot使用JSR-303对参数进行校验

作者: 红烧排骨饭 | 来源:发表于2018-04-16 22:48 被阅读0次

    编写需要校验的model

    import lombok.Data;
    
    import javax.validation.constraints.Max;
    import javax.validation.constraints.Min;
    
    @Data
    public class SchoolDto {
    
        @Min(value = 1, message = "page最小值为1")
        @Max(value = Integer.MAX_VALUE, message = "page过大")
        private Integer page = PageConstant.DEFAULT_PAGE;
    
        @Min(value = 1, message = "pageSize最小值为1")
        @Max(value = Integer.MAX_VALUE, message = "pageSize过大")
        private Integer pageSize = PageConstant.DEFAULT_PAGE_SIZE;
    }
    

    Controller中对参数的校验

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.validation.Valid;
    
    @RestController
    @RequestMapping("/school")
    public class SchoolController {
    
        @Autowired
        private SchoolService schoolService;
    
        @RequestMapping("/list")
        @ResponseBody
        public Result list(@Valid SchoolDto schoolDto, BindingResult bindingResult) {
            if (bindingResult.hasErrors()) {
                return Result.error(ResultEnum.PARAMS_ERROR.getCode(), ValidUtil.getAllErrorMessage(bindingResult));
            }
    
            return Result.success(schoolService.list(schoolDto));
        }
    }
    

    注意

    @Valid 注解的参数后必须紧挨着一个 BindingResult 参数,否则 Spring 会在校验不通过时直接抛出异常

    用到的工具方法

    Controller 返回给客户端的 JSON 对象

    import lombok.Data;
    
    @Data
    public class Result {
    
        private Integer code;
        private String msg;
        private Object data;
    
        public static Result success() {
            return success(null);
        }
    
        public static Result success(Object obj) {
            Result result = new Result();
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg(ResultEnum.SUCCESS.getMsg());
            result.setData(obj);
            return result;
        }
    
        public static Result error(Integer code, String msg) {
            Result result = new Result();
            result.setCode(code);
            result.setMsg(msg);
            return result;
        }
    }
    

    得到参数校验的错误信息

    import org.springframework.validation.BindingResult;
    import org.springframework.validation.ObjectError;
    
    public class ValidUtil {
    
        public static String getAllErrorMessage(BindingResult bindingResult) {
            StringBuilder result = new StringBuilder();
            for (ObjectError error : bindingResult.getAllErrors()) {
                result.append(error.getDefaultMessage()).append(";");
            }
            return result.toString();
        }
    }
    

    相关文章

      网友评论

        本文标题:SpringBoot使用JSR-303对参数进行校验

        本文链接:https://www.haomeiwen.com/subject/igeykftx.html