• spring boot请求参数验证


    场景:

    在接口中经常需要对参数的合法性做验证,spring boot中提供了@Vaild注解,可以方便的完成验证。如何处理验证失败的返回类型,方便客户端调用。

    解决思路:

    在验证参数失败时,会抛出一个MethodArgumentNotValidException的异常,如果我们能捕获这个异常,那就可以把异常信息返回给客户端。

    我们可以使用@RestControllerAdvice注解,对我们的Controller增强,捕获MethodArgumentNotValidException、HttpMessageNotReadableException以及我们自定义的ParamaErrorException。

    实现:

    @RestControllerAdvice("net.kisssoft.controllers")
    public class ControllerExceptionAdvice {
    
        /**
         * 忽略参数异常处理器
         *
         * @param e 忽略参数异常
         * @return ResponseResult
         */
        @ResponseStatus(HttpStatus.BAD_REQUEST)
        @ExceptionHandler(MissingServletRequestParameterException.class)
        public Result parameterMissingExceptionHandler(MissingServletRequestParameterException e) {
            return Result.CreateFail("请求参数 " + e.getParameterName() + " 不能为空");
        }
    
        /**
         * 缺少请求体异常处理器
         *
         * @param e 缺少请求体异常
         * @return ResponseResult
         */
        @ResponseStatus(HttpStatus.BAD_REQUEST)
        @ExceptionHandler(HttpMessageNotReadableException.class)
        public Result parameterBodyMissingExceptionHandler(HttpMessageNotReadableException e) {
            return Result.CreateFail("参数体不能为空");
        }
    
        /**
         * 参数效验异常处理器
         *
         * @param e 参数验证异常
         * @return ResponseInfo
         */
        @ResponseStatus(HttpStatus.BAD_REQUEST)
        @ExceptionHandler(MethodArgumentNotValidException.class)
        public Result parameterExceptionHandler(MethodArgumentNotValidException e) {
            BindingResult exceptions = e.getBindingResult();
            // 判断异常中是否有错误信息,如果存在就使用异常中的消息,否则使用默认消息
            if (exceptions.hasErrors()) {
                List<ObjectError> errors = exceptions.getAllErrors();
                if (!errors.isEmpty()) {
                    // 这里列出了全部错误参数,按正常逻辑,只需要第一条错误即可
                    FieldError fieldError = (FieldError) errors.get(0);
                    return Result.CreateFail(fieldError.getDefaultMessage());
                }
            }
            return Result.CreateFail("error");
        }
    
        /**
         * 自定义参数错误异常处理器
         *
         * @param e 自定义参数
         * @return ResponseInfo
         */
        @ResponseStatus(HttpStatus.BAD_REQUEST)
        @ExceptionHandler({ParamaErrorException.class})
        public Result paramExceptionHandler(ParamaErrorException e) {
            //log.error("", e);
            // 判断异常中是否有错误信息,如果存在就使用异常中的消息,否则使用默认消息
            if (!StringUtils.isEmpty(e.getMessage())) {
                return Result.CreateFail(e.getMessage());
            }
            return Result.CreateFail("error");
        }
    
    }


    Result是我们通用的返回类型,当参数验证失败,我们返回400。并返回对应的类型。


  • 相关阅读:
    WP8微信5.3开始内测 支持Cortana语音 两微破冰了?
    微信公众平台开放设备接入能力 智能手环首批支持
    iPad版微信终于来临了 微信5.4版搜索更智能 转账就是发消息
    消息推送
    微信推广功能支持图片广告和投放外链广告
    微信公众平台增加更多统计项 让你更了解运营数据
    老贼微信公众号运营现状小起底
    Matlab norm 用法小记
    matlab的cell数组
    matlab中元胞数组(cell)转换为矩阵
  • 原文地址:https://www.cnblogs.com/wugang/p/14232348.html
Copyright © 2020-2023  润新知