日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

5. 全局异常捕捉

發布時間:2024/7/5 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 5. 全局异常捕捉 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在一個項目中的異常我們我們都會統一進行處理的,那么如何進行統一進行處理呢?

新建一個類GlobalDefaultExceptionHandler,

在class注解上@ControllerAdvice,

在方法上注解上@ExceptionHandler(value= Exception.class),具體代碼如下:

?

com.kfit.base.exception.GlobalDefaultExceptionHandlerpackage com.kfit.base.exception;importjavax.servlet.http.HttpServletRequest;importorg.springframework.web.bind.annotation.ControllerAdvice;import org.springframework.web.bind.annotation.ExceptionHandler;@ControllerAdvicepublicclass GlobalDefaultExceptionHandler {@ExceptionHandler(value =Exception.class)publicvoiddefaultErrorHandler(HttpServletRequest req, Exception e) {// // If the exception is annotated with@ResponseStatus rethrow it and let// // the framework handle it - like theOrderNotFoundException example// // at the start of this post.// // AnnotationUtils is a Spring Frameworkutility class.// if (AnnotationUtils.findAnnotation(e.getClass(),ResponseStatus.class) != null)// throw e;//// // Otherwise setup and send the user to adefault error-view.// ModelAndView mav = newModelAndView();// mav.addObject("exception", e);// mav.addObject("url",req.getRequestURL());// mav.setViewName(DEFAULT_ERROR_VIEW);// return mav;//打印異常信息: e.printStackTrace();System.out.println("GlobalDefaultExceptionHandler.defaultErrorHandler()");/** 返回json數據或者String數據:* 那么需要在方法上加上注解:@ResponseBody* 添加return即可。*//** 返回視圖:* 定義一個ModelAndView即可,* 然后return;* 定義視圖文件(比如:error.html,error.ftl,error.jsp);**/}}

?

com.kfit.test.web.DemoController?加入方法:

@RequestMapping("/zeroException")publicintzeroException(){return 100/0;}

?

訪問:http://127.0.0.1:8080/zeroException這個方法肯定是拋出異常的,那么在控制臺就可以看到我們全局捕捉的異常信息了。

?

更精確的異常捕捉:

@Controller public class ExceptionHandlingController {// @RequestHandler methods// Exception handling methods// Convert a predefined exception to an HTTP Status code@ResponseStatus(value=HttpStatus.CONFLICT, reason="Data integrity violation") // 409@ExceptionHandler(DataIntegrityViolationException.class)public void conflict() {// Nothing to do }// Specify the name of a specific view that will be used to display the error:@ExceptionHandler({SQLException.class,DataAccessException.class})public String databaseError() {// Nothing to do. Returns the logical view name of an error page, passed to// the view-resolver(s) in usual way.// Note that the exception is _not_ available to this view (it is not added to// the model) but see "Extending ExceptionHandlerExceptionResolver" below.return "databaseError";}// Total control - setup a model and return the view name yourself. Or consider// subclassing ExceptionHandlerExceptionResolver (see below).@ExceptionHandler(Exception.class)public ModelAndView handleError(HttpServletRequest req, Exception exception) {logger.error("Request: " + req.getRequestURL() + " raised " + exception);ModelAndView mav = new ModelAndView();mav.addObject("exception", exception);mav.addObject("url", req.getRequestURL());mav.setViewName("error");return mav;} }

?

轉載于:https://www.cnblogs.com/blackCatFish/p/9886308.html

總結

以上是生活随笔為你收集整理的5. 全局异常捕捉的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。