@RestControllerAdvice @ResponseBody public class GlobalExceptionHandler {
/** * 父类异常处理 */ @ExceptionHandler(Exception.class) public R handleException(Exception e){ return R.fail(ResultCode.FAILURE); }
/** * 参数异常 */ @ExceptionHandler(ArgumentException.class) public R handleArgumentException(ArgumentException e){ return R.fail(e.getResultCode(),e.getMessage()); } /** * 空指针异常 */ @ExceptionHandler(NullPointerException.class) public R handleNullPointerException(NullPointerException e){ return R.fail(ResultCode.FAILURE,"空指针异常"); } }
测试请求
源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
@RestController @RequestMapping("/test/param") public class TestParamController { @GetMapping("not-null") public R testParamNotNull(String name){ ArgumentUtils.notNull(name, ResultCode.PARAM_MISS,"name 不可为空"); return R.success(); } @GetMapping("null-point") public R testNullPoint(Integer age){ int i = age.intValue(); return R.success(); } }