1、springMVC的异常处理
@ControllerAdvice注解例子:
@ControllerAdvice(basePackages = {"com.caiya.ucenter.rest.web.views"}) @ControllerAdvice(assignableTypes = {UserController.class})
更多使用方式参考spring的注解类:
org.springframework.web.bind.annotation.ControllerAdvice
返回视图:
package com.caiya.ucenter.rest.web; import com.alibaba.dubbo.remoting.TimeoutException; import com.caiya.session.SessionException; import com.caiya.ucenter.rest.web.controllers.UserController; import com.caiya.ucenter.rest.web.views.EmailBindConfirmController; import com.caiya.ucenter.service.LoginException; import org.apache.log4j.Logger; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.ModelAndView; import java.lang.reflect.UndeclaredThrowableException; /** * 同步异常处理拦截器,使用了{@link ControllerAdvice}注解 */ @ControllerAdvice(basePackages = {"com.caiya.ucenter.rest.web.views"}) public class ErrorHandler { private static Logger logger = Logger.getLogger(ErrorHandler.class); @ExceptionHandler(value = Exception.class) @ResponseStatus(HttpStatus.OK) public ModelAndView errorResponse(WebRequest request, Exception e) { logger.error(e.getMessage()); ResponseDataWrapper<?> result = null; if(e instanceof IllegalArgumentException){ result = ResponseDataWrapperBuilder.build(request, new ErrorInfo(ErrorInfo.ERROR_ARGUMENT, new StringBuilder("").append(e.getMessage()).toString())); }else if(e instanceof LoginException){ result = ResponseDataWrapperBuilder.build(request, new ErrorInfo(ErrorInfo.ERROR_LOGIN, new StringBuilder("").append(e.getMessage()).toString())); }else if(e instanceof IllegalAccessException){ result = ResponseDataWrapperBuilder.build(request, new ErrorInfo(ErrorInfo.ERROR_ACCESS, new StringBuilder("").append(e.getMessage()).toString()));//非法访问: }else if(e instanceof NullPointerException){ result = ResponseDataWrapperBuilder.build(request, new ErrorInfo(ErrorInfo.ERROR_NULL, new StringBuilder("").append(e.getMessage()).toString())); }else if(e instanceof TimeoutException){ result = ResponseDataWrapperBuilder.build(request, new ErrorInfo(ErrorInfo.ERROR_NULL, new StringBuilder("请求超时,请稍后重试!").append(e.getMessage()).toString())); }else if(e instanceof SessionException){//用户会话失效! result = ResponseDataWrapperBuilder.build(request, new ErrorInfo(ErrorInfo.ERROR_LOGIN, new StringBuilder("").append(e.getMessage()).toString())); }else if(e instanceof UndeclaredThrowableException){//权限 result = ResponseDataWrapperBuilder.build(request, new ErrorInfo(ErrorInfo.ERROR_ACCESS, new StringBuilder("非法访问!").toString()));//非法访问: }else { result = ResponseDataWrapperBuilder.build(request, ErrorInfo.buildSystemError("系统异常,请稍后重试!")); } ModelAndView modelAndView = new ModelAndView("errorpage/500"); modelAndView.addObject("result", result); return modelAndView; } }
返回json:
package com.caiya.ucenter.rest.web; import java.lang.reflect.UndeclaredThrowableException; import com.caiya.session.SessionException; import com.caiya.ucenter.rest.web.controllers.UserController; import com.caiya.ucenter.rest.web.views.EmailBindConfirmController; import org.apache.log4j.Logger; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; import org.springframework.web.context.request.WebRequest; import com.alibaba.dubbo.remoting.TimeoutException; import com.caiya.ucenter.service.LoginException; /** * 异步异常处理拦截器,使用了{@link ControllerAdvice}注解 */ @ControllerAdvice(basePackages = {"com.caiya.ucenter.rest.web.controllers"}) public class RestErrorHandler { private static Logger logger = Logger.getLogger(RestErrorHandler.class); @ExceptionHandler(value = Exception.class) @ResponseStatus(HttpStatus.OK) @ResponseBody public ResponseDataWrapper<?> errorResponse(WebRequest request, Exception e) { logger.error(e.getMessage()); if(e instanceof IllegalArgumentException){ return ResponseDataWrapperBuilder.build(request, new ErrorInfo(ErrorInfo.ERROR_ARGUMENT, new StringBuilder("").append(e.getMessage()).toString())); } if(e instanceof LoginException){ return ResponseDataWrapperBuilder.build(request, new ErrorInfo(ErrorInfo.ERROR_LOGIN, new StringBuilder("").append(e.getMessage()).toString())); } if(e instanceof IllegalAccessException){ return ResponseDataWrapperBuilder.build(request, new ErrorInfo(ErrorInfo.ERROR_ACCESS, new StringBuilder("").append(e.getMessage()).toString()));//非法访问: } if(e instanceof NullPointerException){ return ResponseDataWrapperBuilder.build(request, new ErrorInfo(ErrorInfo.ERROR_NULL, new StringBuilder("").append(e.getMessage()).toString())); } if(e instanceof TimeoutException){ return ResponseDataWrapperBuilder.build(request, new ErrorInfo(ErrorInfo.ERROR_NULL, new StringBuilder("请求超时,请稍后重试!").append(e.getMessage()).toString())); } if(e instanceof SessionException){//用户会话失效! return ResponseDataWrapperBuilder.build(request, new ErrorInfo(ErrorInfo.ERROR_LOGIN, new StringBuilder("").append(e.getMessage()).toString())); } if(e instanceof UndeclaredThrowableException){//权限 return ResponseDataWrapperBuilder.build(request, new ErrorInfo(ErrorInfo.ERROR_ACCESS, new StringBuilder("非法访问!").toString()));//非法访问: } return ResponseDataWrapperBuilder.build(request, ErrorInfo.buildSystemError("系统异常,请稍后重试!")); } }