springboot系列教程(三):全局异常映射(含源码)
创始人
2024-11-04 04:37:47

一、异常分类

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

1、业务异常

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

  • 请输入xxx
  • xxx不能为空
  • xxx重复,请更换

2、系统异常

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

  • 页面丢失404
  • 服务器异常500

二、应用启动后404界面

在这里插入图片描述

1、引入页面Jar包

     org.springframework.boot     spring-boot-starter-thymeleaf  

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","jksadf") ;         return "index";     } } 

3、首页界面

               

三、异常处理

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、统一异常处理格式

两个基础注解

  • @ControllerAdvice 定义统一的异常处理类
  • @ExceptionHandler 定义异常类型对应的处理方式

代码实现

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("出现异常,全体卧倒");     } } 

在这里插入图片描述

相关内容

热门资讯

裸辞做“一人公司”,我后悔了 去年这个时候,一位以色列程序员正在东南亚旅行。他顺手把一个在脑子里转了很久的想法做成了产品,一个让任...
南京建成国内首个Pre-6G试... 4月21日,2026全球6G技术与产业生态大会在南京开幕。全息互动技术展台前,一名远在北京的工作人员...
超梵求职受邀参加“2025抖音... 超梵求职受邀参加“2025抖音巨量引擎成人教育行业生态大会”,探讨分享优质内容传播,服务万千学员。 ...
摩托罗拉Razr 2026(R... IT之家 4 月 22 日消息,摩托罗拉宣布新一代 Razr 折叠手机将于 4 月 29 日在美国发...
库克卸任,特纳斯领航:苹果新纪... 苹果首席执行官蒂姆·库克将卸任,硬件工程主管约翰·特纳斯将接任,苹果公司今天宣布此事。 库克将在夏季...