这篇文章给大家分享的是有关SpringBoot2中如何配置系统全局异常映射处理的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
这里的异常分类从系统处理异常的角度看,主要分类两类:业务异常和系统异常。
业务异常主要是一些可预见性异常,处理业务异常,用来提示用户的操作,提高系统的可操作性。
常见的业务异常提示:
1)请输入xxx
2)xxx不能为空
3)xxx重复,请更换
系统异常主要是一些不可预见性异常,处理系统异常,可以让展示出一个友好的用户界面,不易给用户造成反感。如果是一个金融类系统,在用户界面出现一个系统异常的崩溃界面,很有可能直接导致用户流失。
常见的系统异常提示:
1)页面丢失404
2)服务器异常500
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
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";
}
}
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
<h2 th:text="${name}"></h2>
</body>
</html>
public class ServiceException extends Exception {
public ServiceException (String msg){
super(msg);
}
}
public class ReturnException {
// 响应码
private Integer code;
// 异常描述
private String msg;
// 请求的Url
private String url;
// 省略 get set 方法
}
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 ;
}
}
@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("出现异常,全体卧倒");
}
}
感谢各位的阅读!关于“SpringBoot2中如何配置系统全局异常映射处理”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:http://blog.itpub.net/69957347/viewspace-2670809/