1、springboot Restful使用@ControllerAdvice、@ExceptionHandler、@ResponseBody實現全局異常處理
@ControllerAdvice 註解定義全局異常處理類
@ExceptionHandler 指定自定義錯誤處理方法攔截的異常類型
同一個異常被小範圍的異常類和大範圍的異常處理器同時覆蓋,會選擇小範圍的異常處理器java
1.定義異常業務類web
/** * 異常VO * * @date 2017年2月17日 * @since 1.0.0 */ public class ExceptionVO { private String errorCode; private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getErrorCode() { return errorCode; } public void setErrorCode(String errorCode) { this.errorCode = errorCode; } }
2.定義自定義異常spring
package exception; /** * 無數據Exception * * @date 17/4/25 * @since 1.0.0 */ public class NotFoundException extends SystemException { public NotFoundException(String message) { super(message); } } /** * 系統異常 * * @date 2017年2月12日 * @since 1.0.0 */ public class SystemException extends RuntimeException { private static final long serialVersionUID = 1095242212086237834L; protected Object errorCode; protected Object[] args; public SystemException() { super(); } public SystemException(String message, Throwable cause) { super(message, cause); } public SystemException(String message) { super(message); } public SystemException(String message, Object[] args, Throwable cause) { super(message, cause); this.args = args; } public SystemException(String message, Object[] args) { super(message); this.args = args; } public SystemException(Object errorCode, String message, Throwable cause) { super(message, cause); this.errorCode = errorCode; } public SystemException(Object errorCode, String message) { super(message); this.errorCode = errorCode; } public SystemException(Object errorCode, String message, Object[] args, Throwable cause) { super(message, cause); this.args = args; this.errorCode = errorCode; } public SystemException(Object errorCode, String message, Object[] args) { super(message); this.args = args; this.errorCode = errorCode; } public SystemException(Throwable cause) { super(cause); } public Object[] getArgs() { return args; } public Object getErrorCode() { return errorCode; } }
3.定義全局異常處理類apache
import java.util.HashMap; import java.util.Map; import java.util.stream.Collectors; import NotFoundException; import org.apache.commons.collections.CollectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.context.NoSuchMessageException; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.http.HttpStatus; import org.springframework.validation.BindException; import org.springframework.validation.FieldError; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import ExceptionVO; /** * WEB異常處理器 * * @date 2017年2月16日 * @since 1.0.0 */ @ControllerAdvice("web") //指定異常處理期攔截範圍 public class WebExceptionHandler { static Logger LOG = LoggerFactory.getLogger(WebExceptionHandler.class); @Autowired private MessageSource messageSource; @ExceptionHandler(FieldException.class) @ResponseStatus(HttpStatus.CONFLICT) //指定http響應狀態 @ResponseBody /** * 未找到數據 * * @param e * @return */ @ExceptionHandler(NotFoundException.class)//指定異常類型 @ResponseStatus(HttpStatus.NOT_FOUND) @ResponseBody public ExceptionVO handleNotFoundException(NotFoundException e) { ExceptionVO vo = new ExceptionVO(); fillExceptionVO(e, vo); return vo; } @ExceptionHandler(SystemException.class) @ResponseStatus(HttpStatus.CONFLICT) @ResponseBody public ExceptionVO handleSystemException(SystemException e) { ExceptionVO vo = new ExceptionVO(); fillExceptionVO(e, vo); return vo; } @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(Exception.class) public void globalError(Exception e) { LOG.error(e.getMessage(), e); } /** * 填充異常響應消息 * * @param e * @param vo */ private void fillExceptionVO(SystemException e, ExceptionVO vo) { if (e.getMessage() != null) { String message = e.getMessage(); try { message = messageSource.getMessage(e.getMessage(), e.getArgs(), LocaleContextHolder.getLocale()); } catch (NoSuchMessageException ex) { ; // ignore } vo.setMessage(message); } vo.setErrorCode(String.valueOf(e.getErrorCode())); } }
2、springboot 返回 ModelAndViewspringboot
package exception.handler; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; @Commpent public class OverallExceptionHandler implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception ex) { ModelAndView mav = new ModelAndView(); System.out.println(ex.getMessage()); mav.addObject("errMsg", ex.getMessage()); mav.setViewName("error"); return mav; } }
其它方式:ide
@ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(value = Exception.class) public ModelAndView resolveException(HttpServletRequest request, Exception ex) throws Exception { ModelAndView mav = new ModelAndView(); System.out.println(ex.getMessage()); mav.addObject("errMsg", ex.getMessage()); mav.setViewName("error"); return mav; } }