在一个项目中的异常我们我们都会统一进行处理的,那么如何进行统一进行处理呢?

新建一个类GlobalDefaultExceptionHandler,

在class注解上@ControllerAdvice,

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

com.kfit.base.exception.GlobalDefaultExceptionHandler

packagecom.kfit.base.exception;

importjavax.servlet.http.HttpServletRequest;

importorg.springframework.web.bind.annotation.ControllerAdvice;

import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice

public class GlobalDefaultExceptionHandler{

@ExceptionHandler(value = Exception.class)

public voiddefaultErrorHandler(HttpServletRequest req, Exception e) {

//      // If the exception is annotatedwith @ResponseStatus rethrow it and let

//      // the framework handle it -like the OrderNotFoundException example

//      // at the start of thispost.

//      // AnnotationUtils is aSpring Framework utility class.

//      if (AnnotationUtils.findAnnotation(e.getClass(),ResponseStatus.class) != null)

//          throw e;

//

//      // Otherwise setup and sendthe 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这个方法肯定是抛出异常的,那么在控制台就可以看到我们全局捕捉的异常信息了。

更精确的异常捕捉:

@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;
  }
}

问题描述: 
在使用spring的jdbcTemplate的时候,提示Caused by: java.lang.ClassNotFoundException: org.springframework.dao.DataAccessException错误。 
解决方法:
POM 引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

5.全局异常捕捉【从零开始学Spring Boot】的更多相关文章

  1. (5)全局异常捕捉【从零开始学Spring Boot】

    在一个项目中的异常我们我们都会统一进行处理的,那么如何进行统一进行处理呢? 新建一个类GlobalDefaultExceptionHandler, 在class注解上@ControllerAdvice ...

  2. 68. 使用thymeleaf报异常:Not Found, status=404【从零开始学Spring Boot】

    [从零开始学习Spirng Boot-常见异常汇总] 我们按照正常的流程编码好了 controller访问访问方法/hello,对应的是/templates/hello.html文件,但是在页面中还是 ...

  3. (42)Spring Boot多数据源【从零开始学Spring Boot】

    我们在开发过程中可能需要用到多个数据源,我们有一个项目(MySQL)就是和别的项目(SQL Server)混合使用了.其中SQL Server是别的公司开发的,有些基本数据需要从他们平台进行调取,那么 ...

  4. 57. Spring 自定义properties升级篇【从零开始学Spring Boot】

    之前在两篇文章中都有简单介绍或者提到过 自定义属性的用法: 25.Spring Boot使用自定义的properties[从零开始学Spring Boot] 51. spring boot属性文件之多 ...

  5. 17、Spring Boot普通类调用bean【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/52013017 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个 ...

  6. 21. Spring Boot过滤器、监听器【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/52069490 上一篇文章已经对定义Servlet 的方法进行了说明,过滤器(Filter) ...

  7. 81. Spring Boot集成JSP疑问【从零开始学Spring Boot】

    [原创文章,转载请注明出处] 针对文章: ()Spring Boot 添加JSP支持[从零开始学Spring Boot] 有网友提了这么一些疑问: 1.Spring Boot使用jsp时,仍旧可以打成 ...

  8. 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】

    [原创文章,转载请注明出处] 个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析 ...

  9. 77. Spring Boot Use Thymeleaf 3【从零开始学Spring Boot】

    [原创文章,转载请注明出处] Spring Boot默认选择的Thymeleaf是2.0版本的,那么如果我们就想要使用3.0版本或者说指定版本呢,那么怎么操作呢?在这里要说明下 3.0的配置在spri ...

  10. 75. Spring Boot 定制URL匹配规则【从零开始学Spring Boot】

    在之前有一篇文章说了,博客名称从原来的<从零开始学Spring Boot>更改为<Spring Boot常见异常汇总>,后来写了几篇文章之后发展,有些文章还是一些知识点,所以后 ...

随机推荐

  1. uva1214 Manhattan Wiring 插头DP

    There is a rectangular area containing n × m cells. Two cells are marked with “2”, and another two w ...

  2. Nodejs 网络爬虫(资讯爬虫) 案例

    1. superagent superagent 是一个流行的nodejs第三方模块,专注于处理服务端/客户端的http请求.在nodejs中,我们可以使用内置的http等模块来进行请求的发送.响应处 ...

  3. Poj 2096 Collecting Bugs (概率DP求期望)

    C - Collecting Bugs Time Limit:10000MS     Memory Limit:64000KB     64bit IO Format:%I64d & %I64 ...

  4. Windows下安装Redis并注册为服务

    1.安装 下载地址:https://github.com/MSOpenTech/redis/releases. Redis 支持 32 位和 64 位.这个需要根据你系统平台的实际情况选择,这里我们下 ...

  5. sqlite3数据库 sqlite3_get_table

    上一篇介绍的sqlite3_exec 是使用回调来执行对select结果的操作.还有一个方法可以直接查询而不需要回调.但是,我个人感觉还是回调好,因为代码可以更加整齐,只不过用回调很麻烦,你得声明一个 ...

  6. 软中断网卡处理&Linux高性能外部设备处理机制&SMP

    转载:http://blog.csdn.net/freas_1990/article/details/9238183 看了一些linux网卡驱动的处理技术,对有些概念还是无法理解,突然搜到这篇文章,挺 ...

  7. linux 内核库函数 【转】

    转自:http://blog.chinaunix.net/uid-20321537-id-1966892.html 当编写驱动程序时,一般情况下不能使用C标准库的函数.Linux内核也提供了与标准库函 ...

  8. C 实现删除非空文件夹

    /* 文件名:   rd.c ---------------------------------------------------- c中提供的对文件夹操作的函数,只能对空文件夹进行 删除,这使很多 ...

  9. 右上角X灰化

    CMenu* menu = this->GetSystemMenu(FALSE); menu->EnableMenuItem(SC_CLOSE, MF_BYCOMMAND | MF_GRA ...

  10. 转化一个数字数组为function数组(每个function都弹出相应的数字)

    从汤姆大叔的博客里看到了6个基础题目:本篇是第2题 - 转化一个数字数组为function数组(每个function都弹出相应的数字) 此题关键点: 1.如何将一个匿名函数存入数组? 2.如何锁住需要 ...