本文源码: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. BZOJ 1198 [HNOI2006]军机调度:dfs

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1198 题意: 有n个雇佣军,m个任务. 第i个雇佣军能够参加cnt个任务,分别为temp[ ...

  2. python输入空格间隔的一行int

    str = input() list = [int(x) for x in str.split()] print(list) 用py刷题肯定得遇到空格间隔的键入,先str接收键入的一行字串,然后把st ...

  3. 单机 Oracle 11g(11.2.0.4)手动打补丁PSU(11.2.0.4.8)

    环境说明:database : 11.2.0.4 x64os: centos6.7 x64 准备内容:OPatch : p6880880_112000_Linux-x86-64.zipDB PSU : ...

  4. TTY,Console以及Terminal

    TTY可以理解是一种终端显示.可以在/dev文件夹看到多个tty开头的文件,可以通过alt+Fn(n=1~6)来进行切换.这个是不是和GUI场景下的多个Terminal窗口是一致的呢? 伪TTY是指一 ...

  5. 洛谷【P4883】mzf的考验

    浅谈\(splay\):https://www.cnblogs.com/AKMer/p/9979592.html 浅谈\(fhq\)_\(treap\):https://www.cnblogs.com ...

  6. BZOJ4571:[SCOI2016]美味

    浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem.p ...

  7. 洛谷P2024食物链——并查集补集的灵活运用

    题目:https://www.luogu.org/problemnew/show/P2024 自己在做本题时最大的障碍就是:不会在一个集合的father改变时把相应的补集也跟着改变. 借鉴题解后,才明 ...

  8. Jenkins安装和配置FindBugs、PMD、CheckStyle等插件

    最近研究Jenkins的常用插件的使用,主要使用FindBugs.PMD.CheckStyle.Violations.Emma等插件,主要参考了http://blog.csdn.net/dc_726/ ...

  9. php file_get_contents超时处理

    因为要用php去向我的虚拟主机管理系统发送开通空间等的请求,需要Post传值,由于开通空间过程很慢,同时需要延时处理.以下找到了一下file_get_contents的超时处理,网上有人用2个方法解决 ...

  10. 【239】◀▶IEW-Unit04

    Unit 4 Youth Issues: Computer Use 1 Model1题目及范文分析 Some teenagers spend a lot of time playing compute ...