• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringBoot配置系统全局异常映射处理

springboot 搞代码 4年前 (2022-01-05) 21次浏览 已收录 0个评论
文章目录[隐藏]

在项目开发中,肯定少不了异常的出现,作为后台开发人员,我们总是在不停的写各种接口提供给前端调用,然而不可避免的,当后台出现BUG时,前端总是丑陋的讲错误信息直接暴露给用户,这样的用户体验想必是相当差的,本文主要讲解异常映射的配置

一、异常分类 

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

1、业务异常

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

常见的业务异常提示:
1)请输入xxx
2)xxx不能为空
3)xxx重复,请更换

2、系统异常

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

常见的系统异常提示:
1)页面丢失404
2)服务器异常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","知了一笑") ;         return "index";     } }

3、首页界面

            <title></title> <h1></h1>

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.Mode<b style="color:transparent">来源gao@!dai!ma.com搞$$代^@码网</b>lAndView; 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

以上就是SpringBoot配置系统全局异常映射处理的详细内容,更多关于SpringBoot 异常映射处理的资料请关注gaodaima搞代码网其它相关文章!

以上就是SpringBoot配置系统全局异常映射处理的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot配置系统全局异常映射处理

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址