use of com.rogge.common.core.ApiResponse in project spring-cloud by Rogge666.
the class WebMvcConfigurer method configureHandlerExceptionResolvers.
// 统一异常处理
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
exceptionResolvers.add(new HandlerExceptionResolver() {
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
ApiResponse lApiResponse = new ApiResponse();
if (e instanceof ServiceException) {
// 业务失败的异常,如“账号或密码错误”
lApiResponse.setCode(ResponseCode.Base.ERROR);
lApiResponse.setMsg(e.getMessage());
logger.info(e.getMessage());
} else if (e instanceof NoHandlerFoundException) {
lApiResponse.setCode(ResponseCode.Base.API_NO_EXISTS);
lApiResponse.setMsg("接口 [" + request.getRequestURI() + "] 不存在");
} else if (e instanceof ServletException) {
lApiResponse.setCode(ResponseCode.Base.ERROR);
lApiResponse.setMsg(e.getMessage());
} else if (e.getCause() instanceof TooManyResultsException) {
lApiResponse = ApiResponse.creatFail(ResponseCode.Base.TOO_MANY_EXCEP);
} else {
lApiResponse.setCode(ResponseCode.Base.API_ERR);
lApiResponse.setMsg("接口 [" + request.getRequestURI() + "] 内部错误,请联系管理员");
String message;
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
message = String.format("接口 [%s] 出现异常,方法:%s.%s,异常摘要:%s", request.getRequestURI(), handlerMethod.getBean().getClass().getName(), handlerMethod.getMethod().getName(), e.getMessage());
} else {
message = e.getMessage();
}
logger.error(message, e);
}
responseResult(response, lApiResponse);
return new ModelAndView();
}
});
}
use of com.rogge.common.core.ApiResponse in project spring-cloud by Rogge666.
the class WebMvcConfigurer method configureHandlerExceptionResolvers.
// 统一异常处理
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
exceptionResolvers.add(new HandlerExceptionResolver() {
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
ApiResponse lApiResponse = new ApiResponse();
if (e instanceof ServiceException) {
// 业务失败的异常,如“账号或密码错误”
lApiResponse.setCode(ResponseCode.Base.ERROR);
lApiResponse.setMsg(e.getMessage());
logger.info(e.getMessage());
} else if (e instanceof NoHandlerFoundException) {
lApiResponse.setCode(ResponseCode.Base.API_NO_EXISTS);
lApiResponse.setMsg("接口 [" + request.getRequestURI() + "] 不存在");
} else if (e instanceof ServletException) {
lApiResponse.setCode(ResponseCode.Base.ERROR);
lApiResponse.setMsg(e.getMessage());
} else if (e.getCause() instanceof TooManyResultsException) {
lApiResponse = ApiResponse.creatFail(ResponseCode.Base.TOO_MANY_EXCEP);
} else {
lApiResponse.setCode(ResponseCode.Base.API_ERR);
lApiResponse.setMsg("接口 [" + request.getRequestURI() + "] 内部错误,请联系管理员");
String message;
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
message = String.format("接口 [%s] 出现异常,方法:%s.%s,异常摘要:%s", request.getRequestURI(), handlerMethod.getBean().getClass().getName(), handlerMethod.getMethod().getName(), e.getMessage());
} else {
message = e.getMessage();
}
logger.error(message, e);
}
responseResult(response, lApiResponse);
return new ModelAndView();
}
});
}
use of com.rogge.common.core.ApiResponse in project spring-cloud by Rogge666.
the class WebMvcConfigurer method configureHandlerExceptionResolvers.
// 统一异常处理
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
exceptionResolvers.add(new HandlerExceptionResolver() {
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
ApiResponse lApiResponse = new ApiResponse();
if (e instanceof ServiceException) {
// 业务失败的异常,如“账号或密码错误”
lApiResponse.setCode(ResponseCode.Base.ERROR);
lApiResponse.setMsg(e.getMessage());
logger.info(e.getMessage());
} else if (e instanceof NoHandlerFoundException) {
lApiResponse.setCode(ResponseCode.Base.API_NO_EXISTS);
lApiResponse.setMsg("接口 [" + request.getRequestURI() + "] 不存在");
} else if (e instanceof ServletException) {
lApiResponse.setCode(ResponseCode.Base.ERROR);
lApiResponse.setMsg(e.getMessage());
} else if (e.getCause() instanceof TooManyResultsException) {
lApiResponse = ApiResponse.creatFail(ResponseCode.Base.TOO_MANY_EXCEP);
} else {
lApiResponse.setCode(ResponseCode.Base.API_ERR);
lApiResponse.setMsg("接口 [" + request.getRequestURI() + "] 内部错误,请联系管理员");
String message;
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
message = String.format("接口 [%s] 出现异常,方法:%s.%s,异常摘要:%s", request.getRequestURI(), handlerMethod.getBean().getClass().getName(), handlerMethod.getMethod().getName(), e.getMessage());
} else {
message = e.getMessage();
}
logger.error(message, e);
}
responseResult(response, lApiResponse);
return new ModelAndView();
}
});
}
use of com.rogge.common.core.ApiResponse in project spring-cloud by Rogge666.
the class LoginInterceptor method preHandle.
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 未登录
User user = sessionUserInfo.getCurrentSessionUser(User.class);
if (user == null) {
ApiResponse apiResponse = ApiResponse.creatFail(ResponseCode.LoginRegister.NOLOGIN);
response.setContentType("application/json;charset=UTF-8");
response.getWriter().print(JSON.toJSONString(apiResponse, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteEnumUsingToString));
return false;
} else {
sessionUserInfo.setSessionUser(user);
}
return true;
}
Aggregations