1、后台处理异常

  a、引入thymeleaf依赖

    <!-- thymeleaf模板插件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

  b、在application.properties文件中设置属性

#关闭thymeleaf模板的缓存
spring.thymeleaf.cache=false

  c、编写后台处理Handler  

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; @ControllerAdvice
public class GlobalExceptionHandler {

  //设置此handler处理所有异常
@ExceptionHandler(value=Exception.class)
public void defaultErrorHandler(){
System.out.println("-------------default error");
}
}

  d、后台异常打印

-------------default error
2017-06-16 14:54:05.314 WARN 6892 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.dao.IncorrectResultSizeDataAccessException: result returns more than one elements; nested exception is javax.persistence.NonUniqueResultException: result returns more than one elements

2、页面处理异常

  a、修改Handler

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody; @ControllerAdvice
public class GlobalExceptionHandler { @ExceptionHandler(value=Exception.class)
@ResponseBody
public String defaultErrorHandler(){
System.out.println("-------------default error");
return "系统错误,请联系管理员";
}
}

  b、页面访问结果

    

3、页面处理异常,使用模板页面

  a、编写html模板页面

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<h1 th:inlines="text">异常出现啦</h1>
<p th:text="${messages}"></p>
</body>
</html>

  b、修改Handler

  @ExceptionHandler(value=Exception.class)
public ModelAndView defaultErrorHandler(Exception e){
ModelAndView modal = new ModelAndView();
modal.setViewName("/exception");
modal.addObject("messages", e.getMessage());
return modal;
}

  c、测试结果

Spring Boot入门——全局异常处理的更多相关文章

  1. Spring Boot (8) 全局异常处理

    服务层和dao层的最终异常会交给controller处理,控制层的异常则会记录日志系统. 新建一个类用来处理异常,名字随便GlobalDefaultExceptionHandler.java,加上@C ...

  2. Spring Boot 中全局异常处理器

    Spring Boot 中全局异常处理器,就是把错误异常统一处理的方法.等价于Springmvc中的异常处理器. 步骤一:基于前面的springBoot入门小demo修改 步骤二:修改HelloCon ...

  3. Spring Boot入门(五):使用JDBC访问MySql数据库

    本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 在程序开发的过程中,操作数据库是必不可少的部分,前面几篇博客中,也一直未涉及到数据库的操作,本篇博客 就 ...

  4. Spring Boot 入门案例与配置说明

    一.Spring Boot简介 官网地址:http://spring.io/projects/spring-boot Spring Boot可以轻松创建可以运行的独立的,生产级的基于Spring的应用 ...

  5. Spring Boot 入门之 Web 篇(二)

    原文地址:Spring Boot 入门之 Web 篇(二) 博客地址:http://www.extlight.com 一.前言 上一篇<Spring Boot 入门之基础篇(一)>介绍了 ...

  6. spring boot入门篇

    Spring Boot[快速入门]   Spring Boot 概述 Build Anything with Spring Boot:Spring Boot is the starting point ...

  7. Spring Boot入门学习,解决复杂的spring配置文件及jar包

    转载:https://www.cnblogs.com/wmyskxz/p/9010832.html 总结 为何出了这样的框架? Spring Boot 是所有基于 Spring 开发的项目的起点.Sp ...

  8. Spring Boot 入门 - 目录

    pring Boot 入门 - 进阶篇(3)- 定时任务(@Scheduled) 主要用于定时发送邮件.夜间自动维护等. (1)开启定时任务功能 @Configuration @EnableSched ...

  9. 161103、Spring Boot 入门

    Spring Boot 入门 spring Boot是Spring社区较新的一个项目.该项目的目的是帮助开发者更容易的创建基于Spring的应用程序和服务,让更多人的人更快的对Spring进行入门体验 ...

随机推荐

  1. event对象及各种事件

    事件(event) event对象 (1)什么是event对象? Event 对象代表事件的状态,比如事件在其中发生的元素.键盘按键的状态.鼠标的位置.鼠标按钮的状态.事件通常与函数结合使用,函数不会 ...

  2. DevExpress控件-lookupedit的使用方法详解(图文)转http://blog.csdn.net/qq395537505/article/details/50920508

    绑定数据源: lookupedit.Properties.ValueMember = 实际要用的字段; //相当于editvalue lookupedit.Properties.DisplayMemb ...

  3. MySQL中数据中设计中的范式与反范式

    设计关系数据库时,遵从不同的规范要求,设计出合理的关系型数据库,这些不同的规范要求被称为不同的范式,各种范式呈递次规范,越高的范式数据库冗余越小.但是有些时候一昧的追求范式减少冗余,反而会降低数据读写 ...

  4. MySQL中lock与latch的区分

    这里要区分锁中容易令人混淆的概念lock与latch.在数据库中,lock与latch都可以成为锁,但两者有截然不同的含义 latch 一般称为闩锁(轻量级的锁) 因为其要求锁定的时间非常短,若迟勋时 ...

  5. Python中何时使用断言(转)

    原文:http://blog.jobbole.com/76285/ 本文由 伯乐在线 - 贱圣OMG 翻译.未经许可,禁止转载!英文出处:python maillist.欢迎加入翻译小组. 这个问题是 ...

  6. Spring学习笔记2—AOP

    1.AOP概念 AOP(Aspect Oriented Programming):面向切面编程,AOP能够将那些与业务无关,却为业务模块所共同调用的应用(例如事务处理.日志管理.权限控制等)封装起来, ...

  7. 玩转NumPy数组

    一.Numpy 数值类型 1.前言:Python 本身支持的数值类型有 int(整型, long 长整型).float(浮点型).bool(布尔型) 和 complex(复数型).而 Numpy 支持 ...

  8. 标准c数学函数使用方法

    cppreference.com -> 标准c数学函数 -> 详解 标准c数学函数 abs 语法:     #include <stdlib.h>   int abs( int ...

  9. Loadrunder脚本篇——Run-time Settings之Browser Enmulation

    浏览器模拟 所有Internet Vuser Header包含一个标识将被模拟的浏览器类型(或无线工具包)的User Agent header.例如User-Agent: Mozilla/3.01Go ...

  10. 每天一个Linux命令(53)service命令

        service命令用于对系统服务进行管理.   (1)用法:     用法:  service  [服务]  [操作]   (2)功能:     功能:  service命令用于启动.停止.重 ...