fixed: handler 可能为非HandlerMethod 类型的异常

master
jacobs 5 years ago
parent 74055a5390
commit 19708ebeb6
  1. 128
      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; package com.xxl.job.admin.controller.resolver;
import com.xxl.job.admin.core.exception.XxlJobException; import com.xxl.job.admin.core.exception.XxlJobException;
import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.admin.core.util.JacksonUtil; import com.xxl.job.admin.core.util.JacksonUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod; import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
/** /**
* common exception resolver * common exception resolver
* *
* @author xuxueli 2016-1-6 19:22:18 * @author xuxueli 2016-1-6 19:22:18
*/ */
@Component @Component
public class WebExceptionResolver implements HandlerExceptionResolver { public class WebExceptionResolver implements HandlerExceptionResolver {
private static transient Logger logger = LoggerFactory.getLogger(WebExceptionResolver.class); private static transient Logger logger = LoggerFactory.getLogger(WebExceptionResolver.class);
@Override @Override
public ModelAndView resolveException(HttpServletRequest request, public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) { HttpServletResponse response, Object handler, Exception ex) {
if (!(ex instanceof XxlJobException)) { if (!(ex instanceof XxlJobException)) {
logger.error("WebExceptionResolver:{}", ex); logger.error("WebExceptionResolver:{}", ex);
} }
// if json // if json
boolean isJson = false; boolean isJson = false;
HandlerMethod method = (HandlerMethod)handler; if (handler instanceof HandlerMethod) {
ResponseBody responseBody = method.getMethodAnnotation(ResponseBody.class); HandlerMethod method = (HandlerMethod)handler;
if (responseBody != null) { ResponseBody responseBody = method.getMethodAnnotation(ResponseBody.class);
isJson = true; if (responseBody != null) {
} isJson = true;
}
// error result }
ReturnT<String> errorResult = new ReturnT<String>(ReturnT.FAIL_CODE, ex.toString().replaceAll("\n", "<br/>"));
// error result
// response ReturnT<String> errorResult = new ReturnT<String>(ReturnT.FAIL_CODE, ex.toString().replaceAll("\n", "<br/>"));
ModelAndView mv = new ModelAndView();
if (isJson) { // response
try { ModelAndView mv = new ModelAndView();
response.setContentType("application/json;charset=utf-8"); if (isJson) {
response.getWriter().print(JacksonUtil.writeValueAsString(errorResult)); try {
} catch (IOException e) { response.setContentType("application/json;charset=utf-8");
logger.error(e.getMessage(), e); response.getWriter().print(JacksonUtil.writeValueAsString(errorResult));
} } catch (IOException e) {
return mv; logger.error(e.getMessage(), e);
} else { }
return mv;
mv.addObject("exceptionMsg", errorResult.getMsg()); } else {
mv.setViewName("/common/common.exception");
return mv; mv.addObject("exceptionMsg", errorResult.getMsg());
} mv.setViewName("/common/common.exception");
} return mv;
}
}
} }
Loading…
Cancel
Save