diff --git a/platform-common/src/main/java/com/lyms/platform/common/base/ExceptionHandlerController.java b/platform-common/src/main/java/com/lyms/platform/common/base/ExceptionHandlerController.java index a60ec09..bbc1314 100644 --- a/platform-common/src/main/java/com/lyms/platform/common/base/ExceptionHandlerController.java +++ b/platform-common/src/main/java/com/lyms/platform/common/base/ExceptionHandlerController.java @@ -7,7 +7,6 @@ import com.lyms.platform.common.exception.ParameterException; import com.lyms.platform.common.exception.TokenException; import com.lyms.platform.common.result.BaseResponse; import com.lyms.platform.common.utils.ExceptionUtils; -import com.lyms.platform.common.utils.JsonUtil; import org.springframework.aop.AopInvocationException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessResourceFailureException; @@ -56,6 +55,7 @@ public class ExceptionHandlerController { error.setErrormsg("错误"); return error; } + @ExceptionHandler(org.springframework.aop.AopInvocationException.class) @ResponseBody @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @@ -70,102 +70,82 @@ public class ExceptionHandlerController { @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseStatus(HttpStatus.OK) - public void validationError(MethodArgumentNotValidException e, HttpServletResponse httpServletResponse) { - try { - setHttpResponseHeader(httpServletResponse); + public Map validationError(MethodArgumentNotValidException e, HttpServletResponse httpServletResponse) { + setHttpResponseHeader(httpServletResponse); + + return createValidationResponse(e.getBindingResult()); - httpServletResponse.getWriter().print(JsonUtil.obj2JsonString(createValidationResponse(e.getBindingResult()))); - } catch (Exception ex) { - } } @ExceptionHandler(BindException.class) @ResponseStatus(HttpStatus.OK) - public void validationError(BindException e, HttpServletResponse httpServletResponse) { - try { - setHttpResponseHeader(httpServletResponse); + public Map validationError(BindException e, HttpServletResponse httpServletResponse) { + setHttpResponseHeader(httpServletResponse); + + return createValidationResponse(e.getBindingResult()); - httpServletResponse.getWriter().print(JsonUtil.obj2JsonString(createValidationResponse(e.getBindingResult()))); - } catch (Exception ex) { - } } @ResponseStatus(HttpStatus.OK) @ExceptionHandler(ParameterException.class) - public void validationError(HttpServletResponse httpServletResponse) { - try { - setHttpResponseHeader(httpServletResponse); - Map resultMap = new HashMap<>(); - resultMap.put("errorcode", ErrorCodeConstants.PARAMETER_ERROR); - resultMap.put("errormsg", "参数错误."); - httpServletResponse.getWriter().print(JsonUtil.obj2JsonString(resultMap)); - } catch (Exception ex) { - ExceptionUtils.catchException(ex, "ExceptionHandlerController validationError."); - } + @ResponseBody + public Map validationError(HttpServletResponse httpServletResponse) { + setHttpResponseHeader(httpServletResponse); + Map resultMap = new HashMap<>(); + resultMap.put("errorcode", ErrorCodeConstants.PARAMETER_ERROR); + resultMap.put("errormsg", "参数错误."); + return resultMap; } @ResponseStatus(HttpStatus.OK) @ExceptionHandler(TokenException.class) - public void buildTokenExpire(HttpServletResponse httpServletResponse) { - try { - setHttpResponseHeader(httpServletResponse); - Map resultMap = new HashMap<>(); - resultMap.put("errorcode", ErrorCodeConstants.TOKEN_EXPIRE); - resultMap.put("errormsg", "您当前用户登录状态异常,暂时无法访问,请重新登录"); - httpServletResponse.getWriter().print(JsonUtil.obj2JsonString(resultMap)); - } catch (Exception ex) { - ExceptionUtils.catchException(ex, "ExceptionHandlerController buildTokenExpire."); - } + public Map buildTokenExpire(HttpServletResponse httpServletResponse) { + setHttpResponseHeader(httpServletResponse); + Map resultMap = new HashMap<>(); + resultMap.put("errorcode", ErrorCodeConstants.TOKEN_EXPIRE); + resultMap.put("errormsg", "您当前用户登录状态异常,暂时无法访问,请重新登录"); + return resultMap; } /** * 处理spring mvc 缺少请求参数的异常提示 - * + * * @param httpServletResponse * @param missRequestParameter */ @ResponseStatus(HttpStatus.OK) @ExceptionHandler(org.springframework.web.bind.MissingServletRequestParameterException.class) - public void handlMissingServletRequestParameter(HttpServletResponse httpServletResponse, - MissingServletRequestParameterException missRequestParameter) { - try { - setHttpResponseHeader(httpServletResponse); - Map resultMap = new HashMap<>(); - resultMap.put("errorcode", ErrorCodeConstants.PARAMETER_ERROR); - resultMap.put("errormsg", missRequestParameter.getMessage()); - httpServletResponse.getWriter().print(JsonUtil.obj2JsonString(resultMap)); - } catch (Exception ex) { - ExceptionUtils.catchException(ex, "ExceptionHandlerController handlMissingServletRequestParameter."); - } + public Map handlMissingServletRequestParameter(HttpServletResponse httpServletResponse, + MissingServletRequestParameterException missRequestParameter) { + setHttpResponseHeader(httpServletResponse); + Map resultMap = new HashMap<>(); + resultMap.put("errorcode", ErrorCodeConstants.PARAMETER_ERROR); + resultMap.put("errormsg", missRequestParameter.getMessage()); + ExceptionUtils.catchException(missRequestParameter, "ExceptionHandlerController handlMissingServletRequestParameter."); + return resultMap; } @ResponseStatus(HttpStatus.OK) @ExceptionHandler(BusinessException.class) - public void handlBusinessException(HttpServletResponse httpServletResponse, BusinessException busExc) { - try { - setHttpResponseHeader(httpServletResponse); - Map resultMap = new HashMap<>(); - resultMap.put("errorcode", ErrorCodeConstants.BUSINESS_ERROR); - resultMap.put("errormsg", busExc.getMessage()); - httpServletResponse.getWriter().print(JsonUtil.obj2JsonString(resultMap)); - } catch (Exception ex) { - ExceptionUtils.catchException(ex, "ExceptionHandlerController Write response With handException."); - } + public Map handlBusinessException(HttpServletResponse httpServletResponse, BusinessException busExc) { + setHttpResponseHeader(httpServletResponse); + Map resultMap = new HashMap<>(); + resultMap.put("errorcode", ErrorCodeConstants.BUSINESS_ERROR); + resultMap.put("errormsg", busExc.getMessage()); + ExceptionUtils.catchException(busExc, "ExceptionHandlerController Write response With handException."); + return resultMap; } @ResponseStatus(HttpStatus.OK) @ExceptionHandler(Exception.class) - public void buildException(HttpServletResponse httpServletResponse, Exception e,HttpServletRequest httpServletRequest) { - try { - setHttpResponseHeader(httpServletResponse); - Map resultMap = new HashMap<>(); - resultMap.put("errorcode", ErrorCodeConstants.SYSTEM_ERROR); - resultMap.put("errormsg", "服务器异常."); - httpServletResponse.getWriter().print(JsonUtil.obj2JsonString(resultMap)); - ExceptionUtils.catchException(e, "ExceptionHandlerController Exception. queryStr: "+httpServletRequest.getQueryString()); - } catch (Exception ex) { - ExceptionUtils.catchException(ex, "ExceptionHandlerController Write response With handException."); - } + public Map buildException(HttpServletResponse httpServletResponse, Exception e, HttpServletRequest httpServletRequest) { + setHttpResponseHeader(httpServletResponse); + Map resultMap = new HashMap<>(); + resultMap.put("errorcode", ErrorCodeConstants.SYSTEM_ERROR); + resultMap.put("errormsg", "服务器异常."); + ExceptionUtils.catchException(e, "ExceptionHandlerController Exception. queryStr: " + httpServletRequest.getQueryString()); + + return resultMap; } private Map createValidationResponse(BindingResult bindingResult) { @@ -173,7 +153,7 @@ public class ExceptionHandlerController { List fieldErrors = bindingResult.getFieldErrors(); for (org.springframework.validation.FieldError fieldError : fieldErrors) { map.put("errorcode", ErrorCodeConstants.PARAMETER_ERROR); - map.put("errormsg",messageResolver.getErrorMessage(fieldError + map.put("errormsg", messageResolver.getErrorMessage(fieldError .getDefaultMessage())); break; }