From 820b4407d60f9b31b04bcd7522502dc0cbd68fdd Mon Sep 17 00:00:00 2001 From: xuxueli <931591021@qq.com> Date: Sat, 20 Jan 2018 11:56:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BD=E9=99=85=E5=8C=96=E5=AE=9A=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resolver/WebExceptionResolver.java | 30 ++++++++++++++----- xxl-job-admin/src/main/webapp/500.html | 7 +++-- .../WEB-INF/template/common/common.result.ftl | 1 - .../src/main/webapp/WEB-INF/template/help.ftl | 2 +- 4 files changed, 28 insertions(+), 12 deletions(-) delete mode 100644 xxl-job-admin/src/main/webapp/WEB-INF/template/common/common.result.ftl diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/controller/resolver/WebExceptionResolver.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/controller/resolver/WebExceptionResolver.java index e8928f5f..0ceb9089 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/controller/resolver/WebExceptionResolver.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/controller/resolver/WebExceptionResolver.java @@ -11,6 +11,7 @@ import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.IOException; /** * common exception resolver @@ -23,19 +24,34 @@ public class WebExceptionResolver implements HandlerExceptionResolver { public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { logger.error("WebExceptionResolver:{}", ex); - - ModelAndView mv = new ModelAndView(); + + // if json + boolean isJson = false; HandlerMethod method = (HandlerMethod)handler; ResponseBody responseBody = method.getMethodAnnotation(ResponseBody.class); if (responseBody != null) { - response.setContentType("application/json;charset=UTF-8"); - mv.addObject("result", JacksonUtil.writeValueAsString(new ReturnT(500, ex.toString().replaceAll("\n", "
")))); - mv.setViewName("/common/common.result"); + isJson = true; + } + + // error result + ReturnT errorResult = new ReturnT(ReturnT.FAIL_CODE, ex.toString().replaceAll("\n", "
")); + + // response + ModelAndView mv = new ModelAndView(); + if (isJson) { + try { + response.setContentType("application/json;charset=utf-8"); + response.getWriter().print(JacksonUtil.writeValueAsString(errorResult)); + } catch (IOException e) { + logger.error(e.getMessage(), e); + } + return mv; } else { - mv.addObject("exceptionMsg", ex.toString().replaceAll("\n", "
")); + + mv.addObject("exceptionMsg", errorResult.getMsg()); mv.setViewName("/common/common.exception"); + return mv; } - return mv; } } \ No newline at end of file diff --git a/xxl-job-admin/src/main/webapp/500.html b/xxl-job-admin/src/main/webapp/500.html index ed32d54f..5cf0a73c 100644 --- a/xxl-job-admin/src/main/webapp/500.html +++ b/xxl-job-admin/src/main/webapp/500.html @@ -2,7 +2,7 @@ - 应用程序异常 (500) + Error