spring boot学习教程(4):全局异常处理代码demo
2018-01-04 16:16
1136 查看
在一个项目中的异常我们我们都会统一进行处理的,那么如何进行统一进行处理呢?
新建一个类GlobalDefaultExceptionHandler,
在class注解上@ControllerAdvice,
在方法上注解上@ExceptionHandler(value = Exception.class),具体代码如下:
package com.kfit.base.exception;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
publicclass GlobalDefaultExceptionHandler {
// // If the exception is annotated with @ResponseStatus rethrow it and let
// // the framework handle it - like the OrderNotFoundException example
// // at the start of this post.
// // AnnotationUtils is a Spring Framework utility class.
// if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null)
// throw e;
//
// // Otherwise setup and send the user to a default error-view.
// ModelAndView mav = new ModelAndView();
// mav.addObject(“exception”, e);
// mav.addObject(“url”, req.getRequestURL());
// mav.setViewName(DEFAULT_ERROR_VIEW);
// return mav;
}
}
com.kfit.test.web.DemoController 加入方法:
@RequestMapping(“/zeroException”)
public int zeroException(){
return 100/0;
}
访问:http://127.0.0.1:8080/zeroException 这个方法肯定是抛出异常的,那么在控制台就可以看到我们全局捕捉的异常信息了
新建一个类GlobalDefaultExceptionHandler,
在class注解上@ControllerAdvice,
在方法上注解上@ExceptionHandler(value = Exception.class),具体代码如下:
package com.kfit.base.exception;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
publicclass GlobalDefaultExceptionHandler {
@ExceptionHandler(value = Exception.class) publicvoid defaultErrorHandler(HttpServletRequest req, Exception e) {
// // If the exception is annotated with @ResponseStatus rethrow it and let
// // the framework handle it - like the OrderNotFoundException example
// // at the start of this post.
// // AnnotationUtils is a Spring Framework utility class.
// if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null)
// throw e;
//
// // Otherwise setup and send the user to a default error-view.
// ModelAndView mav = new ModelAndView();
// 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”)
public int zeroException(){
return 100/0;
}
访问:http://127.0.0.1:8080/zeroException 这个方法肯定是抛出异常的,那么在控制台就可以看到我们全局捕捉的异常信息了
相关文章推荐
- spring boot之全局异常处理代码demo
- SpringBoot学习之全局异常处理设置(返回JSON)
- spring boot 学习--03---web控制层全局异常处理
- SpringBoot学习历程(4)->全局异常的处理
- SpringBoot学习——全局异常处理设置(返回JSON)
- SpringBoot初始教程之统一异常处理(三)
- SpringBoot系列之三全局异常的捕获处理
- Spring Boot @ControllerAdvice 处理全局异常,返回固定格式Json
- SpringBoot全局异常处理
- SpringBoot入门——局部与全局的异常处理
- spring boot全局异常处理
- spring boot 教程(四) 统一异常处理
- Spring boot 七 全局异常处理
- Spring-Boot(五) 全局异常处理
- Spring Boot学习(六)之Web应用的统一异常处理
- Spring boot 全局异常处理
- Spring Boot全局异常处理解析
- springboot-20-全局异常处理
- SpringBoot全局异常处理
- Spring Boot 全局异常处理