springboot實(shí)現(xiàn)全局異常處理及自定義異常類
全局異常處理及自定義異常類
全局異常處理
定義一個處理類,使用@ControllerAdvice注解。
@ControllerAdvice注解:控制器增強(qiáng),一個被@Component注冊的組件。
配合@ExceptionHandler來增強(qiáng)所有的@requestMapping方法。
例如:@ExceptionHandler(Exception.class) 用來捕獲@requestMapping的方法中所有拋出的exception。
代碼:
@ControllerAdvice public class GlobalDefultExceptionHandler {?? ? ?? ?//聲明要捕獲的異常 ?? ?@ExceptionHandler(Exception.class) ?? ?@ResponseBody ?? ?public String defultExcepitonHandler(HttpServletRequest request,Exception e) { ?? ? ? ?return “error”; ?? ?} }
這樣,全局異常處理類完畢??梢蕴砑幼约旱倪壿嫛?/p>
然后還有一個問題,有的時候,我們需要業(yè)務(wù)邏輯時拋出自定義異常,這個時候需要自定義業(yè)務(wù)異常類。
定義class:BusinessException ,使他繼承于RuntimeException.
說明:因?yàn)槟承I(yè)務(wù)需要進(jìn)行業(yè)務(wù)回滾。但spring的事務(wù)只針對RuntimeException的進(jìn)行回滾操作。所以需要回滾就要繼承RuntimeException。
public class BusinessException extends RuntimeException{? }
然后,現(xiàn)在來稍微完善一下這個類。
當(dāng)我們拋出一個業(yè)務(wù)異常,一般需要錯誤碼和錯誤信息。有助于我們來定位問題。
所以如下:
public class BusinessException extends RuntimeException{ ?? ?//自定義錯誤碼 ?? ?private Integer code; ?? ?//自定義構(gòu)造器,只保留一個,讓其必須輸入錯誤碼及內(nèi)容 ?? ?public BusinessException(int code,String msg) { ?? ??? ?super(msg); ?? ??? ?this.code = code; ?? ?} ? ?? ?public Integer getCode() { ?? ??? ?return code; ?? ?} ? ?? ?public void setCode(Integer code) { ?? ??? ?this.code = code; ?? ?} }
這時候,我們發(fā)現(xiàn)還有一個問題,如果這樣寫,在代碼多起來以后,很難管理這些業(yè)務(wù)異常和錯誤碼之間的匹配。所以在優(yōu)化一下。
把錯誤碼及錯誤信息,組裝起來統(tǒng)一管理。
定義一個業(yè)務(wù)異常的枚舉
public enum ResultEnum { ?? ?UNKONW_ERROR(-1,"未知錯誤"), ?? ?SUCCESS(0,"成功"), ?? ?ERROR(1,"失敗"), ?? ?;?? ? ?? ?private Integer code; ?? ?private String msg;?? ? ?? ?ResultEnum(Integer code,String msg) { ?? ??? ?this.code = code; ?? ??? ?this.msg = msg; ?? ?} ? ?? ?public Integer getCode() { ?? ??? ?return code; ?? ?} ? ?? ?public String getMsg() { ?? ??? ?return msg; ?? ?} }
這個時候,業(yè)務(wù)異常類:
public class BusinessException extends RuntimeException{?? ? ?? ?private static final long serialVersionUID = 1L;?? ? ?? ?private Integer code; ?//錯誤碼? ?? ?public BusinessException() {}?? ? ?? ?public BusinessException(ResultEnum resultEnum) { ?? ??? ?super(resultEnum.getMsg()); ?? ??? ?this.code = resultEnum.getCode(); ?? ?} ?? ? ?? ?public Integer getCode() { ?? ??? ?return code; ?? ?} ? ?? ?public void setCode(Integer code) { ?? ??? ?this.code = code; ?? ?} }
然后再修改一下全局異常處理類:
@ControllerAdvice public class GlobalDefultExceptionHandler { ?? ? ?? ?//聲明要捕獲的異常 ?? ?@ExceptionHandler(Exception.class) ?? ?@ResponseBody ?? ?public <T> Result<?> defultExcepitonHandler(HttpServletRequest request,Exception e) { ?? ??? ?e.printStackTrace(); ?? ??? ?if(e instanceof BusinessException) { ?? ??? ??? ?Log.error(this.getClass(),"業(yè)務(wù)異常:"+e.getMessage()); ?? ??? ??? ?BusinessException businessException = (BusinessException)e; ?? ??? ??? ?return ResultUtil.error(businessException.getCode(), businessException.getMessage()); ?? ??? ?} ?? ??? ?//未知錯誤 ?? ??? ?return ResultUtil.error(-1, "系統(tǒng)異常:\\n"+e); ?? ?}?? ? }
判斷這個是否是業(yè)務(wù)異常。和系統(tǒng)異常就可以分開處理了。
全局異常處理配置
springboot Restful使用
@ControllerAdvice、@ExceptionHandler、@ResponseBody實(shí)現(xiàn)全局異常處理
@ControllerAdvice
注解定義全局異常處理類@ExceptionHandler
指定自定義錯誤處理方法攔截的異常類型
同一個異常被小范圍的異常類和大范圍的異常處理器同時覆蓋,會選擇小范圍的異常處理器
1.定義異常業(yè)務(wù)類
/** * 異常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.定義自定義異常
package exception; /** * 無數(shù)據(jù)Exception * * @date 17/4/25 * @since 1.0.0 */ public class NotFoundException extends SystemException { public NotFoundException(String message) { super(message); } } /** * 系統(tǒng)異常 * * @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.定義全局異常處理類
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響應(yīng)狀態(tài) @ResponseBody /** * 未找到數(shù)據(jù) * * @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); } /** * 填充異常響應(yīng)消息 * * @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())); } }
springboot 返回 ModelAndView
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; } }
其它方式:
@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; ? ? ? } }
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java使用RandomAccessFile類基于指針讀寫文件實(shí)例代碼
這篇文章主要介紹了java使用RandomAccessFile類基于指針讀寫文件實(shí)例代碼,具有一定參考價值,需要的朋友可以了解下。2017-10-10java設(shè)計(jì)模式之觀察者模式學(xué)習(xí)
這篇文章主要為大家詳細(xì)介紹了java設(shè)計(jì)模式之觀察者模式,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01JVM自定義類加載器在代碼擴(kuò)展性實(shí)踐分享
這篇文章主要介紹了JVM自定義類加載器在代碼擴(kuò)展性實(shí)踐分享,一個類型從被加載到虛擬機(jī)內(nèi)存中開始,到卸載出內(nèi)存為止,它的整個生命周期將會經(jīng)歷加載、驗(yàn)證、準(zhǔn)備、解析、初始化 、使用和卸載七個階段,其中驗(yàn)證、準(zhǔn)備、解析三個部分統(tǒng)稱為連接2022-06-06mybatis interceptor 處理查詢參數(shù)及查詢結(jié)果的實(shí)例代碼
這篇文章主要介紹了mybatis interceptor 處理查詢參數(shù)及查詢結(jié)果,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-01-01tio-http-server打包為二進(jìn)制文件的實(shí)現(xiàn)及優(yōu)勢詳解
這篇文章主要為大家介紹了tio-http-server打包為二進(jìn)制文件實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12深入學(xué)習(xí)java中的Groovy 和 Scala 類
本文將探討三種下一代 JVM 語言:Groovy、Scala 和 Clojure,比較并對比新的功能和范例,讓 Java 開發(fā)人員對自己近期的未來發(fā)展有大體的認(rèn)識。,需要的朋友可以參考下2019-06-06Java Socket實(shí)現(xiàn)的傳輸對象功能示例
這篇文章主要介紹了Java Socket實(shí)現(xiàn)的傳輸對象功能,結(jié)合具體實(shí)例形式分析了java socket傳輸對象的原理及接口、客戶端、服務(wù)器端相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06Java中的ScheduledThreadPoolExecutor定時任務(wù)詳解
這篇文章主要介紹了Java中的ScheduledThreadPoolExecutor詳解,??ScheduledThreadPoolExecutor?繼承自?ThreadPoolExecutor,它主要用來在給定的延遲之后運(yùn)行任務(wù),或者定期執(zhí)行任務(wù),ScheduledThreadPoolExecutor?的功能與?Timer?類似<BR>,需要的朋友可以參考下2023-12-12Debian配置JDK1.7 與Linux Java Helloworld
這篇文章主要介紹了Debian配置JDK1.7 與Linux Java Helloworld 的相關(guān)資料,需要的朋友可以參考下2016-06-06