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

新建一个类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. c#字典序

    using System; using System.Collections.Generic; public class Example { public static void Main() { / ...

  2. odp.net 读写oracle blob字段

    DEVELOPER: ODP.NET Serving Winning LOBs: http://www.oracle.com/technetwork/issue-archive/2005/05-nov ...

  3. appium+python自动化26-模拟手势点击坐标(tap)【转载】

    ​# 前言:有时候定位元素的时候,你使出了十八班武艺还是定位不到,怎么办呢?(面试经常会问)那就拿出绝招:点元素所在位置的坐标 tap用法 1.tap是模拟手指点击,一般页面上元素的语法有两个参数,第 ...

  4. poj 3164(最小树形图模板)

    题目链接:http://poj.org/problem?id=3164 详细可以看这里:http://www.cnblogs.com/vongang/archive/2012/07/18/259685 ...

  5. IIS——MIME介绍与添加MIME类型

    MIME(MultipurposeInternet Mail Extensions)多用途互联网邮件扩展类型.是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会 ...

  6. [thinkphp] 如何解析ajaxReturn返回的json字符串

    success: function(data){ var dataObj=eval("("+data+")");//转换为json对象 alert(dataOb ...

  7. HDU 2016.11.12 做题感想

    细数一下这两天做过的值得总结的一些题Orz...... HDU 2571 简单dp,但是一开始WA了一发.原因很简单:没有考虑仔细. 如果指向该点的所有点权值都为负数,那就错了(我一开始默认初始值为0 ...

  8. Linux CURL的安装

    Linux CURL的安装  Linux CURL的安装   --获得安装包,从网上直接下载或者其他途径,这里直接wget# wget http://curl.haxx.se/download/cur ...

  9. VMware给虚拟机绑定物理网卡

    前言: 桥接模式:就是使用真实的IP地址 NAT模式:使用以VMnet 8所指定的子网中分配的IP地址,在外网信息交互中不存在这样的IP. 仅主机模式:仅用于虚拟机与真机之间的信息交互. 操作步骤: ...

  10. sqlserver 下载地址(SQL Server 2008 R2 中英文 开发版/企业版/标准版 下载)

    转自:http://blog.sina.com.cn/s/blog_624b1f950100pioh.html   注:企业版无法安装在xp和win7,开发版才可以! 一. 简体中文 1. SQL S ...