springboot 默认异常处理
SpringBoot默认有自定义异常处理的体系,在做SpringBoot项目的时候,如果是抛出了运行时异常,springBoot并会对异常进行处理,返回如下异常信息:
{"timestamp": 1517294278132,"status": 500,"error": "Internal Server Error","exception": "com.lgy.common.exception.BusinessException","message": "[001]自定义的uncheck 异常!","path": "/validateExceptionTest" }
追究其原因,发现SpirngBoot出现异常信息时候,会默认访问/error,springBoot种有BasicErrorController这个类来处理异常信息:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.springframework.boot.autoconfigure.web;import java.util.Collections; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeStacktrace; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.util.Assert; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView;@Controller @RequestMapping({"${server.error.path:${error.path:/error}}"}) public class BasicErrorController extends AbstractErrorController {private final ErrorProperties errorProperties;public BasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties) {this(errorAttributes, errorProperties, Collections.emptyList());}public BasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties, List<ErrorViewResolver> errorViewResolvers) {super(errorAttributes, errorViewResolvers);Assert.notNull(errorProperties, "ErrorProperties must not be null");this.errorProperties = errorProperties;}public String getErrorPath() {return this.errorProperties.getPath();}@RequestMapping(produces = {"text/html"})public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {HttpStatus status = this.getStatus(request);Map<String, Object> model = Collections.unmodifiableMap(this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.TEXT_HTML)));response.setStatus(status.value());ModelAndView modelAndView = this.resolveErrorView(request, response, status, model);return modelAndView == null?new ModelAndView("error", model):modelAndView;}@RequestMapping@ResponseBodypublic ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {Map<String, Object> body = this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.ALL));HttpStatus status = this.getStatus(request);return new ResponseEntity(body, status);}protected boolean isIncludeStackTrace(HttpServletRequest request, MediaType produces) {IncludeStacktrace include = this.getErrorProperties().getIncludeStacktrace();return include == IncludeStacktrace.ALWAYS?true:(include == IncludeStacktrace.ON_TRACE_PARAM?this.getTraceParameter(request):false);}protected ErrorProperties getErrorProperties() {return this.errorProperties;} }
如果要取代SpringBoot默认的异常处理信息方式,继承ErrorController:
package com.lgy.controller;import com.alibaba.fastjson.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.web.BasicErrorController; import org.springframework.boot.autoconfigure.web.ErrorAttributes; import org.springframework.boot.autoconfigure.web.ErrorController; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest; import java.util.Map;/*** Created by fengch on 2018/1/30.*/ @Controller @RequestMapping("${server.error.path:${error.path:/error}}") public class FundaErrorController implements ErrorController {private static final String PATH = "/error";@Autowiredprivate ErrorAttributes errorAttributes;@Overridepublic String getErrorPath() {return PATH;}@RequestMapping@ResponseBodypublic JSONObject doHandleError(HttpServletRequest request) {RequestAttributes requestAttributes = new ServletRequestAttributes(request);Map<String,Object> errorAttributesData = errorAttributes.getErrorAttributes(requestAttributes,true);Integer status=(Integer)errorAttributesData.get("status"); //状态码String path=(String)errorAttributesData.get("path"); //请求路径String messageFound=(String)errorAttributesData.get("message"); //异常信息 JSONObject reData = new JSONObject();reData.put("status_", status);reData.put("path_", path);reData.put("message", messageFound);return reData;} }
接着在访问,并是自定义处理的异常信息了:
{"message": "[001]自定义的uncheck 异常!","path_": "/validateExceptionTest","status_": 500 }
【转载】
springboot 默认异常处理相关推荐
- java默认异常处理_spring boot 默认异常处理的实现
本周在看陈杰写的自定义异常的微信异常时,使用的是自定义异常状态码和信息,在出错时将他抛出,并用@ExceptionHandler注解定义一个全局异常处理器,根据异常的内容向前台发送状态码和信息,处理异 ...
- SpringBoot自适应异常处理
效果演示 我们先来看一下Springboot的默认效果 浏览器访问 客户端访问 划重点!!! 但是绝大部分公司的代码,都是没做自适应处理的,很大一部分原因在于,你在网上搜索Springboot全局异常 ...
- springboot统一异常处理类及注解参数为数组的写法
springboot统一异常处理类及注解参数为数组的写法 参考文章: (1)springboot统一异常处理类及注解参数为数组的写法 (2)https://www.cnblogs.com/zhucww ...
- springboot自定义异常处理
springboot自定义异常处理 参考文章: (1)springboot自定义异常处理 (2)https://www.cnblogs.com/SimpleWu/p/10044468.html 备忘一 ...
- SpringBoot_日志-SpringBoot默认配
咱们说了springboot做日志管理的依赖原理,用logback的方式进行实现,那怎么用呢 大家测试一下,我们打开之前创建的工程,现在不做任何配置的情况下,我就直接来运行这段代码,我们看到控制台其实 ...
- Springboot默认加载application.yml原理
Springboot默认加载application.yml原理以及扩展 SpringApplication.run(-)默认会加载classpath下的application.yml或applicat ...
- SpringBoot默认日志logback配置解析
SpringBoot默认日志logback配置解析 前言 今天来介绍下Spring Boot如何配置日志logback,我刚学习的时候,是带着下面几个问题来查资料的,你呢 如何引入日志? 日志输出格式 ...
- SpringBoot默认包扫描机制及@ComponentScan指定扫描路径详解
SpringBoot默认包扫描机制及@ComponentScan指定扫描路径详解 SpringBoot默认包扫描机制 标注了@Component和@Component的衍生注解如@Controller ...
- SpringBoot默认日志配置输出级别
点击关注公众号,实用技术文章及时了解 来源:blog.csdn.net/csdn18740599042 /article/details/109031005 Springboot默认配置 我们在测试类 ...
最新文章
- HDFS读写过程解析
- MFC最小程序(不使用应用程序向导)
- kafka高性能揭秘:顺序写和零拷贝
- oracle低权限下获取shell
- KVM虚拟机迁移原理分析
- webpack4与babel配合使es6代码可运行于低版本浏览器
- python word处理_Python 处理word期间遇到的问题
- mysql 升序和降序
- 东南大学计算机esl排名,不是江苏考生,你不会知道,这所被戏称福建的“三本”,有多难考...
- C#生成格林威治时间字符串
- 令人头痛的WH_CBT钩子,使窗口前置——泪水+汗水的赞歌
- 啥?Grafana 还能为日志添加告警?
- 使用有道词典API做一个简单的翻译页面 HTML+JS+有道词典API(代码可直接运行)
- Java用20行代码实现抖音小视频批量转换为gif动态图【值得收藏】
- jsvmp-某乎_x-zes-96参数算法还原(手把手教学)
- SonicWALL防火墙配置NAT Policy
- 偏微分方程(Partial Differential Equation III)
- 《人人都是产品经理》一书作者推荐的国内产品书籍列表
- composer 升级后,Class ‘Facade\Ignition\IgnitionServiceProvider‘ not found
- codeforces131B
热门文章
- python3 测试函数的一个例子
- linux快捷命令补齐,Linux Shell简介——自动补齐/命令行的历史记录/编辑命令行/可用的 Shell 快捷方式.doc...
- zip解压mysql安装图解_Mysql安装教程-zip格式压缩包
- Leetcode题库 4.寻找两个正序数组的中位数(双指针法 C实现)
- I/O复用函数的使用——epoll
- c++内存管理-分配失败
- java 序列化 clone_利用java序列化进行对象深Clone
- 二.Sql语言的分类及运算符
- 课堂练习之四则运算加强版
- 手动清除后门程序Iexplores.exe