5.全局异常捕捉【从零开始学Spring Boot】
在一个项目中的异常我们我们都会统一进行处理的,那么如何进行统一进行处理呢?
新建一个类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】的更多相关文章
- (5)全局异常捕捉【从零开始学Spring Boot】
在一个项目中的异常我们我们都会统一进行处理的,那么如何进行统一进行处理呢? 新建一个类GlobalDefaultExceptionHandler, 在class注解上@ControllerAdvice ...
- 68. 使用thymeleaf报异常:Not Found, status=404【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] 我们按照正常的流程编码好了 controller访问访问方法/hello,对应的是/templates/hello.html文件,但是在页面中还是 ...
- (42)Spring Boot多数据源【从零开始学Spring Boot】
我们在开发过程中可能需要用到多个数据源,我们有一个项目(MySQL)就是和别的项目(SQL Server)混合使用了.其中SQL Server是别的公司开发的,有些基本数据需要从他们平台进行调取,那么 ...
- 57. Spring 自定义properties升级篇【从零开始学Spring Boot】
之前在两篇文章中都有简单介绍或者提到过 自定义属性的用法: 25.Spring Boot使用自定义的properties[从零开始学Spring Boot] 51. spring boot属性文件之多 ...
- 17、Spring Boot普通类调用bean【从零开始学Spring Boot】
转载:http://blog.csdn.net/linxingliang/article/details/52013017 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个 ...
- 21. Spring Boot过滤器、监听器【从零开始学Spring Boot】
转载:http://blog.csdn.net/linxingliang/article/details/52069490 上一篇文章已经对定义Servlet 的方法进行了说明,过滤器(Filter) ...
- 81. Spring Boot集成JSP疑问【从零开始学Spring Boot】
[原创文章,转载请注明出处] 针对文章: ()Spring Boot 添加JSP支持[从零开始学Spring Boot] 有网友提了这么一些疑问: 1.Spring Boot使用jsp时,仍旧可以打成 ...
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
[原创文章,转载请注明出处] 个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析 ...
- 77. Spring Boot Use Thymeleaf 3【从零开始学Spring Boot】
[原创文章,转载请注明出处] Spring Boot默认选择的Thymeleaf是2.0版本的,那么如果我们就想要使用3.0版本或者说指定版本呢,那么怎么操作呢?在这里要说明下 3.0的配置在spri ...
- 75. Spring Boot 定制URL匹配规则【从零开始学Spring Boot】
在之前有一篇文章说了,博客名称从原来的<从零开始学Spring Boot>更改为<Spring Boot常见异常汇总>,后来写了几篇文章之后发展,有些文章还是一些知识点,所以后 ...
随机推荐
- angular.extend(dst,src)的简单示例
自我认为这个方法跟angular.copy(src,dst)有点相似.在angular.extend({},src)时,就可以画等号.这个src只代表一个对象.代码如下:(注意这个src可以有多个对象 ...
- OpenCV 2.4.9 学习笔记(3)—— OpenCV自动为输出数组(矩阵)分配内存
OpenCV大部分时候会为OpenCV方法中的输出数据(方法的参数)自动分配内存,所以如果一个方法的参数有一个或者多个输入数组(cv::Mat 实例)和一些输出数组时,OpenCV会自动为输出数组分配 ...
- CString和string在unicode与非unicode下的相互转换(转)
原文转自 http://blog.csdn.net/u014303844/article/details/51397556 CString和string在unicode与非unicode下的相互转换 ...
- MyRecycleView带有上拉加载更多
package com.gan.myrecycleview; import android.content.Context; import android.support.v4.widget.Swip ...
- http请求分析
一个Http请求一般始于如下几种方式: 1.在浏览器中输入一个URL地址 2.网页中的一个超链接 3.Response.Redirect("http://www.sohu.com" ...
- 基于UDP高性能传输协议UDT doc翻译(一)
原文转自:http://hi.baidu.com/doodlezone/item/74a203155efe26dbbf9042dd UDT文档阅读理解 一. 概述 ...
- c#操作SQL的例子
>> 数据表复制 当表目标表存在时: insert into 目的数据库..表 select * from 源数据库..表 当目标表不存在时: select * into 目的数据库..表 ...
- poj 3164(最小树形图模板)
题目链接:http://poj.org/problem?id=3164 详细可以看这里:http://www.cnblogs.com/vongang/archive/2012/07/18/259685 ...
- (12)oracle事务
事物 http://www.cnblogs.com/linjiqin/archive/2012/02/06/2340637.htm 在当前的事务中设置保存点 savepoint 名字; 保存点回滚 ...
- Codeforces Gym 101775D Mr. Panda and Geometric Sequence(2017-2018 ACM-ICPC Asia East Continent League Final,D题,枚举剪枝)
题目链接 ECL-Final 2017 Problem D 题意 给定$2*10^{5}$组询问,每个询问求$l$到$r$之间有多少个符合条件的数 如果一个数小于等于$10^{15}$, 并且能被 ...