From 19708ebeb658f46123681853ef8da16612a2f3b1 Mon Sep 17 00:00:00 2001 From: jacobs Date: Mon, 4 May 2020 11:04:11 +0800 Subject: [PATCH] =?UTF-8?q?fixed:=20handler=20=E5=8F=AF=E8=83=BD=E4=B8=BA?= =?UTF-8?q?=E9=9D=9EHandlerMethod=20=E7=B1=BB=E5=9E=8B=E7=9A=84=E5=BC=82?= =?UTF-8?q?=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resolver/WebExceptionResolver.java | 128 +++++++++--------- 1 file changed, 65 insertions(+), 63 deletions(-) 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 a0b3e6a6..114407b6 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 @@ -1,64 +1,66 @@ -package com.xxl.job.admin.controller.resolver; - -import com.xxl.job.admin.core.exception.XxlJobException; -import com.xxl.job.core.biz.model.ReturnT; -import com.xxl.job.admin.core.util.JacksonUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.method.HandlerMethod; -import org.springframework.web.servlet.HandlerExceptionResolver; -import org.springframework.web.servlet.ModelAndView; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; - -/** - * common exception resolver - * - * @author xuxueli 2016-1-6 19:22:18 - */ -@Component -public class WebExceptionResolver implements HandlerExceptionResolver { - private static transient Logger logger = LoggerFactory.getLogger(WebExceptionResolver.class); - - @Override - public ModelAndView resolveException(HttpServletRequest request, - HttpServletResponse response, Object handler, Exception ex) { - - if (!(ex instanceof XxlJobException)) { - logger.error("WebExceptionResolver:{}", ex); - } - - // if json - boolean isJson = false; - HandlerMethod method = (HandlerMethod)handler; - ResponseBody responseBody = method.getMethodAnnotation(ResponseBody.class); - if (responseBody != null) { - 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", errorResult.getMsg()); - mv.setViewName("/common/common.exception"); - return mv; - } - } - +package com.xxl.job.admin.controller.resolver; + +import com.xxl.job.admin.core.exception.XxlJobException; +import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.admin.core.util.JacksonUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.method.HandlerMethod; +import org.springframework.web.servlet.HandlerExceptionResolver; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * common exception resolver + * + * @author xuxueli 2016-1-6 19:22:18 + */ +@Component +public class WebExceptionResolver implements HandlerExceptionResolver { + private static transient Logger logger = LoggerFactory.getLogger(WebExceptionResolver.class); + + @Override + public ModelAndView resolveException(HttpServletRequest request, + HttpServletResponse response, Object handler, Exception ex) { + + if (!(ex instanceof XxlJobException)) { + logger.error("WebExceptionResolver:{}", ex); + } + + // if json + boolean isJson = false; + if (handler instanceof HandlerMethod) { + HandlerMethod method = (HandlerMethod)handler; + ResponseBody responseBody = method.getMethodAnnotation(ResponseBody.class); + if (responseBody != null) { + 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", errorResult.getMsg()); + mv.setViewName("/common/common.exception"); + return mv; + } + } + } \ No newline at end of file