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
總結
- 上一篇: openstack-r版(rocky)搭
- 下一篇: 001.Parted工具使用