背景

在开发测试的时候,把接口文档甩给其他人员的时候,经常会在业务报警群里看到这样的报错

xx
dev
59142027E864A81BE39BB95073D5A43
exceptionHandler, handler error:Required String parameter 'xxxxxx' is not present
xx
dev
0BAF1B927DBE49D9B411CA31423C7A82
exceptionHandler, handler error:Required request body is missing: public com.xx.support.result.Result<com.xx.pm.api.response.xxxxxResp> com.xx.xx.api.controller.xxxxController.edit(com.xx.xx.api.request.xxxxVO)

这样的报错对非开发人员不友好,往往不知道这是什么错误,需要将其进行改为业务报错
但是,这样的报错往往在没有进入业务代码的时候就抛异常了。

处理方法

在全局异常Handler中加入如下代码

@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseBody
public Result handleMissingRequestBody(HttpMessageNotReadableException ex) {
    log.error("请求体缺失异常:{}", ex.getMessage(), ex);
    // Todo:这里具体的写法,看你发挥了!!
    String msg = "请求体缺失:请按文档传入请求体后重试。";
    DingAlarmUtils.alarmException(msg);
    return Result.buildErrorResult(msg);
}

@ExceptionHandler(MissingServletRequestParameterException.class)
@ResponseBody
public Result<?> handleMissingServletRequestParameterException(MissingServletRequestParameterException ex) {
    log.error("请求参数缺失异常:{}", ex.getMessage(), ex);
    // Todo:这里具体的写法,看你发挥了!!
    String msg = "请求参数缺失:请按文档传入请求体后重试。";
    DingAlarmUtils.alarmException(msg);
    return Result.buildErrorResult(msg);
}

全部异常捕获代码

小生听雨园此处内容已经被作者隐藏,请输入验证码查看内容
验证码:
请关注本站微信公众号,回复“验证码”,获取验证码。在微信里搜索“小生听雨园”或者“manners_maketh_man__”或者微信扫描右侧二维码都可以关注本站微信公众号。

站点统计

  • 文章总数:309 篇
  • 分类总数:19 个
  • 标签总数:191 个
  • 运行天数:1009 天
  • 访问总数:129727 人次

浙公网安备33011302000604

辽ICP备20003309号