自己抛出的Runtime异常,返回的msg是null
来源:6-3 接口请求全局异常处理-设计与验证

慕斯卡5022575
2020-08-12
debug跑,值都取到了,为什么到返回页面的时候是null呢?
没找到哪里错了,重新对比代码也没发现问题。
ex = {RuntimeException@5792} "java.lang.RuntimeException: test exception"
url = "/test/hello.json"
defaultMsg = "System error"
result = {JsonData@5821}
ret = false
msg = null
data = null
2回答
-
慕斯卡5022575
提问者
2021-01-12
@Controller
@RequestMapping("/test")
@Slf4j
public class TestController {
@RequestMapping("/hello.json")
@ResponseBody
public JsonData hello(){
log.info("hello");
throw new RuntimeException("test exception");
//return JsonData.success("hello,permission");
}
}
@Slf4j
public class SpringExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
//返回结果
String url = request.getRequestURI().toString();
ModelAndView mv;
//定义全局异常信息
String defaultMsg = "System error";
//对数据请求和页面请求做处理
//判断是数据请求还是页面请求(以后缀判断.json,.page)
if (url.endsWith(".json")) { //要求项目中所有请求json数据都以.json结尾
if (ex instanceof PermissionException) {
JsonData result = JsonData.fail(ex.getMessage());
mv = new ModelAndView("jsonView", result.toMap());
} else {
log.error("unknown json exception,url:" + url,ex);
JsonData result = JsonData.fail(defaultMsg);
mv = new ModelAndView("jsonView", result.toMap());
}
} else if (url.endsWith(".page")){ //要求项目中所有请求page页面都以.page结尾
log.error("unknown page exception,url:" + url,ex);
JsonData result = JsonData.fail(defaultMsg);
mv = new ModelAndView("exception",result.toMap());
} else {
//全局默认处理
log.error("unknown exception,url:" + url,ex);
JsonData result = JsonData.fail(defaultMsg);
mv = new ModelAndView("jsonView",result.toMap());
}
return mv;
}
}
00 -
Jimin
2020-08-12
你好,抛出异常的代码和全局异常拦截的代码发来看看,以及你的JsonData类
022021-01-12
相似问题