1本文源码 2GitHub:知了一笑 3https://github.com/cicadasmile/spring-boot-base
一、异常分类
这里的异常分类从系统处理异常的角度看,主要分类两类:业务异常和系统异常。
1、业务异常
业务异常主要是一些可预见性异常,处理业务异常,用来提示用户的操作,提高系统的可操作性。常见的业务异常提示:
11)请输入xxx 22)xxx不能为空 33)xxx重复,请更换
2、系统异常
系统异常主要是一些不可预见性异常,处理系统异常,可以让展示出一个友好的用户界面,不易给用户造成反感。如果是一个金融类系统,在用户界面出现一个系统异常的崩溃界面,很有可能直接导致用户流失。 常见的系统异常提示:
11)页面丢失404 22)服务器异常500
二、解决应用启动后404界面

1、引入页面Jar包
1<dependency> 2 <groupid>org.springframework.boot</groupid> 3 <artifactid>spring-boot-starter-thymeleaf</artifactid> 4</dependency>
2、自定义首页接口
1import org.springframework.stereotype.Controller; 2import org.springframework.ui.ModelMap; 3import org.springframework.web.bind.annotation.RequestMapping; 4@Controller 5public class IndexController { 6 @RequestMapping("/") 7 public String index(ModelMap modelMap) { 8 modelMap.addAttribute("name","知了一笑") ; 9 return "index"; 10 } 11}
3、首页界面
1 <meta charset="UTF-8"> 2 <title></title> 3 4 5<h1 th:text="${name}"></h1> 6 7
4、运行效果

三、SpringBoot2.0中异常处理
1、项目结构图

2、自定义业务异常类
1public class ServiceException extends Exception { 2 public ServiceException (String msg){ 3 super(msg); 4 } 5}
3、自定义异常描述对象
1public class ReturnException { 2 // 响应码 3 private Integer code; 4 // 异常描述 5 private String msg; 6 // 请求的Url 7 private String url; 8 // 省略 get set 方法 9}
4、统一异常处理格式
1)两个基础注解
@ControllerAdvice 定义统一的异常处理类
@ExceptionHandler 定义异常类型对应的处理方式
2)代码实现
1import org.springframework.web.bind.annotation.ControllerAdvice; 2import org.springframework.web.bind.annotation.ExceptionHandler; 3import org.springframework.web.bind.annotation.ResponseBody; 4import org.springframework.web.servlet.ModelAndView; 5import javax.servlet.http.HttpServletRequest; 6@ControllerAdvice 7// 异常以Json格式返回 等同 ExceptionHandler + ResponseBody 注解 8// @RestControllerAdvice 9public class HandlerException { 10 /** 11 * 自定义业务异常映射,返回JSON格式提示 12 */ 13 @ExceptionHandler(value = ServiceException.class) 14 @ResponseBody 15 public ReturnException handler01 (HttpServletRequest request,ServiceException e){ 16 ReturnException returnException = new ReturnException() ; 17 returnException.setCode(600); 18 returnException.setMsg(e.getMessage()); 19 returnException.setUrl(String.valueOf(request.getRequestURL())); 20 return returnException ; 21 } 22 /** 23 * 服务异常 24 */ 25 @ExceptionHandler(value = Exception.class) 26 public ModelAndView handler02 (HttpServletRequest request,Exception e){ 27 ModelAndView modelAndView = new ModelAndView() ; 28 modelAndView.addObject("ExeMsg", e.getMessage()); 29 modelAndView.addObject("ReqUrl", request.getRequestURL()); 30 modelAndView.setViewName("/exemsg"); 31 return modelAndView ; 32 } 33}
5、简单的测试接口
1@Controller 2public class ExeController { 3 /** 4 * { 5 * "code": 600, 6 * "msg": "业务异常:ID 不能为空", 7 * "url": "http://localhost:8003/exception01" 8 * } 9 */ 10 @RequestMapping("/exception01") 11 public String exception01 () throws ServiceException { 12 throw new ServiceException("业务异常:ID 不能为空"); 13 } 14 15 @RequestMapping("/exception02") 16 public String exception02 () throws Exception { 17 throw new Exception("出现异常,全体卧倒"); 18 } 19}

四、源代码地址
1GitHub地址:知了一笑 2https://github.com/cicadasmile/spring-boot-base 3码云地址:知了一笑 4https://gitee.com/cicadasmile/spring-boot-base
