本文源码:GitHub·点这里 || GitEE·点这里

一、异常分类

这里的异常分类从系统处理异常的角度看,主要分类两类:业务异常和系统异常。

1、业务异常

业务异常主要是一些可预见性异常,处理业务异常,用来提示用户的操作,提高系统的可操作性。

常见的业务异常提示:

1)请输入xxx

2)xxx不能为空

3)xxx重复,请更换

2、系统异常

系统异常主要是一些不可预见性异常,处理系统异常,可以让展示出一个友好的用户界面,不易给用户造成反感。如果是一个金融类系统,在用户界面出现一个系统异常的崩溃界面,很有可能直接导致用户流失。

常见的系统异常提示:

1)页面丢失404

2)服务器异常500

二、解决应用启动后404界面

1、引入页面Jar包

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2、自定义首页接口

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping("/")
public String index(ModelMap modelMap) {
modelMap.addAttribute("name","知了一笑") ;
return "index";
}
}

3、首页界面

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
<h1 th:text="${name}"></h1>
</body>
</html>

4、运行效果

三、SpringBoot2.0中异常处理

1、项目结构图

2、自定义业务异常类

public class ServiceException extends Exception {
public ServiceException (String msg){
super(msg);
}
}

3、自定义异常描述对象

public class ReturnException {
// 响应码
private Integer code;
// 异常描述
private String msg;
// 请求的Url
private String url;
// 省略 get set 方法
}

4、统一异常处理格式

1)两个基础注解

@ControllerAdvice 定义统一的异常处理类

@ExceptionHandler 定义异常类型对应的处理方式

2)代码实现

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
@ControllerAdvice
// 异常以Json格式返回 等同 ExceptionHandler + ResponseBody 注解
// @RestControllerAdvice
public class HandlerException {
/**
* 自定义业务异常映射,返回JSON格式提示
*/
@ExceptionHandler(value = ServiceException.class)
@ResponseBody
public ReturnException handler01 (HttpServletRequest request,ServiceException e){
ReturnException returnException = new ReturnException() ;
returnException.setCode(600);
returnException.setMsg(e.getMessage());
returnException.setUrl(String.valueOf(request.getRequestURL()));
return returnException ;
}
/**
* 服务异常
*/
@ExceptionHandler(value = Exception.class)
public ModelAndView handler02 (HttpServletRequest request,Exception e){
ModelAndView modelAndView = new ModelAndView() ;
modelAndView.addObject("ExeMsg", e.getMessage());
modelAndView.addObject("ReqUrl", request.getRequestURL());
modelAndView.setViewName("/exemsg");
return modelAndView ;
}
}

5、简单的测试接口

@Controller
public class ExeController {
/**
* {
* "code": 600,
* "msg": "业务异常:ID 不能为空",
* "url": "http://localhost:8003/exception01"
* }
*/
@RequestMapping("/exception01")
public String exception01 () throws ServiceException {
throw new ServiceException("业务异常:ID 不能为空");
} @RequestMapping("/exception02")
public String exception02 () throws Exception {
throw new Exception("出现异常,全体卧倒");
}
}

四、源代码地址

GitHub·地址
https://github.com/cicadasmile/spring-boot-base
GitEE·地址
https://gitee.com/cicadasmile/spring-boot-base

SpringBoot2.0 基础案例(03):配置系统全局异常映射处理的更多相关文章

  1. SpringBoot2.0 基础案例(12):基于转账案例,演示事务管理操作

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.事务管理简介 1.事务基本概念 一组业务操作ABCD,要么全部 ...

  2. 三:SpringBoot-配置系统全局异常映射处理

    三:SpringBoot-配置系统全局异常映射处理 1.异常分类 1.1 业务异常 1.2 系统异常 2.自定义异常处理 2.1 自定义业务异常类 2.2 自定义异常描述对象 2.3 统一异常处理格式 ...

  3. SpringBoot2.0 基础案例(16):配置Actuator组件,实现系统监控

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.Actuator简介 1.监控组件作用 在生产环境中,需要实时 ...

  4. SpringBoot2.0 基础案例(14):基于Yml配置方式,实现文件上传逻辑

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.文件上传 文件上传是项目开发中一个很常用的功能,常见的如头像上 ...

  5. SpringBoot2.0 基础案例(07):集成Druid连接池,配置监控界面

    一.Druid连接池 1.druid简介 Druid连接池是阿里巴巴开源的数据库连接池项目.Druid连接池为监控而生,内置强大的监控功能,监控特性不影响性能.功能强大,能防SQL注入,内置Login ...

  6. SpringBoot2.0 基础案例(11):配置AOP切面编程,解决日志记录业务

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.AOP切面编程 1.什么是AOP编程 在软件业,AOP为Asp ...

  7. SpringBoot2.0 基础案例(15):配置MongoDB数据库,实现增删改查逻辑

    本文源码:GitHub·点这里 || GitEE·点这里 一.NoSQL简介 1.NoSQL 概念 NoSQL( Not Only SQL ),意即"不仅仅是SQL".对不同于传统 ...

  8. SpringBoot2.0 基础案例(05):多个拦截器配置和使用场景

    一.拦截器简介 1.拦截器定义 拦截器,请求的接口被访问之前,进行拦截然后在之前或之后加入某些操作.拦截是AOP的一种实现策略. 拦截器主要用来按照指定规则拒绝请求. 2.拦截器中应用 Token令牌 ...

  9. SpringBoot2.0基础案例(01):环境搭建和RestFul风格接口

    一.SpringBoot 框架的特点 1.SpringBoot2.0 特点 1)SpringBoot继承了Spring优秀的基因,上手难度小 2)简化配置,提供各种默认配置来简化项目配置 3)内嵌式容 ...

随机推荐

  1. ubuntu16.04下安装wordpress

    安装过程 http://blog.topspeedsnail.com/archives/4635 http://blog.topspeedsnail.com/archives/4646 碰到的问题 1 ...

  2. 算法(Algorithms)第4版 练习 1.3.9

    主要思路: 用Dijkstra的双栈算法. 遇到数字则压入数字栈中(String). 遇到运算符则压入运算符栈中(String). 遇到右括号时,从数字栈和运算法栈中弹出相应的元素,生成相应的运算表达 ...

  3. ZOJ 3640 Help Me Escape:期望dp

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3640 题意: 有一个吸血鬼被困住了,他要逃跑... 他面前有n条 ...

  4. sublime 相关配置和快捷键

    1.安装package control  点击sublime的菜单栏 view->show console :现在打开了控制台, 这个控制台有上下两栏, 上面一栏会实时显示sublime执行了什 ...

  5. ES禁用_source不会影响聚合

    From Elasticsearch's website: The _source field contains the original JSON document body that was pa ...

  6. Java_Time_01_获取当前时间

    1. Date SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// ...

  7. codeforces 705B B. Spider Man(组合游戏)

    题目链接: B. Spider Man time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. #基础概念#之tensor

    中译名:张量 定义: from wiki: In mathematics, tensors are geometric objects that describe linear relations b ...

  9. Collaborative Index Embedding for Image Retrieval

    最近看了一篇比较好的文章,效果很好,简单记录一下. 这篇文章的核心思想是,融合两种不同类型的特征.文章中用的是SIFT和CNN提取的特征.还是神经大法好啊. 第一步就是建立两种不同特征的索引,文章用的 ...

  10. 牛客网暑期ACM多校训练营(第三场)G:Coloring Tree(函数的思想)

    之前两次遇到过函数的思想的题,所以这次很敏感就看出来了.可以参考之前的题: https://www.cnblogs.com/hua-dong/p/9291507.html Christmas is c ...