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