Spring Boot中Restful Api的异常统一处理

我们在用Spring Boot去向前端提供Restful Api接口时,经常会遇到接口处理异常的情况,产生异常的可能原因是参数错误,空指针异常,SQL执行错误等等。

当发生这些异常时,Spring Boot会自动对异常进行一次统一的处理,返回一个异常信息:

@RestController
public class LiveRestController {
 
 
    // 用户查询某一种直播状态下的所有直播信息
    @GetMapping("/lives")
    public List<LiveResponse> getLiveList(@RequestParam Long status) {
        // 这里特意写上.toString()是为了触发NPE
        log.info("get status:{} lives", status.toString());
        return Lists.newArrayList();
    }
 
 
}

当不带参数向这个接口发起请求时就会得到下面的结果:
MVyBOs.png

这样的结果对于用户来说是很不友好的,前端也并不能够向用户展示这样的错误信息。

我们可以看到这个异常的处理日志如下:

2019-10-15 23:03:25.351  WARN 31571 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required Long parameter 'status' is not present]

这个就是由Spring Boot内部异常处理类来处理的结果。对此,我们需要去实现定制自己的异常处理类,已实现更好的异常提示。

首先要创建全局异常处理类,就需要用到两个重要的注解:

@ControllerAdvice定义统一的异常处理类,这样就不必在每个Controller中逐个定义AOP去拦截处理异常。
@ExceptionHandler用来定义函数针对的异常类型,最后将Exception对象处理成自己想要的结果。

下面给出自定义异常处理的示例代码:

// 注解定义异常统一处理类
@ControllerAdvice
public class RestfulApiExceptionHandler {
 
    // 这里处理MissingServletRequestParameterException的异常
    @ExceptionHandler(value = MissingServletRequestParameterException.class)
    // 返回JSON数据
    @ResponseBody
    // 特别注意第二个参数为要处理的异常
    public Map<String, Object> requestExceptionHandler(HttpServletRequest request, MissingServletRequestParameterException exception) {
        Map<String, Object> error = Maps.newHashMap();
        error.put("status", 500);
        // 在这里可以定义返回的异常提示信息
        error.put("message", "参数" + exception.getParameterName() + "错误");
        return error;
    }
 
    // 这里处理没有被上一个方法处理的异常,通常在最后的方法处理最大的Exception,保证兜底
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Map<String, Object> exceptionHandler(HttpServletRequest request, Exception exception) throws Exception {
        Map<String, Object> error = Maps.newHashMap();
        error.put("status", 500);
        error.put("message", "系统错误");
        return error;
    }
 
}

如此异常信息会被处理成:
MVyImR.png

可以看到异常信息被改成了我们自定义的形式,但是这个时候的HTTP状态码被处理成了200,我们可以通过定义的status来完成错误请求的识别与处理。

posted @ 2019-11-08 14:53  听雨阁中听雨歌  Views(1953)  Comments(0Edit  收藏  举报