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