springBoot中怎么自定义异常响应
发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,这篇文章将为大家详细讲解有关springBoot中怎么自定义异常响应,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。1 自定义异常返回json数据、//
千家信息网最后更新 2025年02月02日springBoot中怎么自定义异常响应
这篇文章将为大家详细讲解有关springBoot中怎么自定义异常响应,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
1 自定义异常返回json数据、
//不要忘记添加 @ControllerAdvice
@ControllerAdvice
public class myExceptionHandler {
//浏览器和客户端返回的都是json// @ResponseBody
@ExceptionHandler(UserNotExist.class)
public MaphandlerException(Exception e){ Map map = new HashMap<>(); map.put("code","user.notexist"); map.put("message",e.getMessage()); return map;
} }
2 转发到/error下,进行自适应响应 @ControllerAdvice public class myExceptionHandler {
//浏览器和客户端返回的都是json@ExceptionHandler(UserNotExist.class)public String handlerException(Exception e, HttpServletRequest request){ Mapmap = new HashMap<>(); request.setAttribute("javax.servlet.error.status_code",500); map.put("code","user.notexist"); map.put("message",e.getMessage()); request.setAttribute("ext",map); return "forward:/error"; }
}
3 将定制的错误携带出去 @Component
public class myErrorAtrributes extends DefaultErrorAttributes {
//WebRequest webRequest,注意1.x.x版本的写法与2.1.0写法不同,我的是2.1.0public MapgetErrorAttributes(WebRequest webRequest, boolean includeStackTrace) { Map errorAttributes = super.getErrorAttributes(webRequest,includeStackTrace); errorAttributes.put("com", "maodong"); Map ext= (Map ) webRequest.getAttribute("ext",0); errorAttributes.put("ext",ext); return errorAttributes; }
}
//为啥这么写:父类DefaultErrorAttributes类中的方法
public Map
MaperrorAttributes = new LinkedHashMap(); errorAttributes.put("timestamp", new Date()); this.addStatus(errorAttributes, webRequest); this.addErrorDetails(errorAttributes, webRequest, includeStackTrace); this.addPath(errorAttributes, webRequest); return errorAttributes;}//为啥WebRequest webRequest能得到与低版本RequestAttrributes相同的结果public interface WebRequest extends RequestAttributes {@NullableString getHeader(String var1);@NullableString[] getHeaderValues(String var1);Iterator getHeaderNames();------}//为啥 Map ext= (Map ) webRequest.getAttribute("ext",0);能获得extpublic interface RequestAttributes {int SCOPE_REQUEST = 0;int SCOPE_SESSION = 1;String REFERENCE_REQUEST = "request";String REFERENCE_SESSION = "session";@NullableObject getAttribute(String var1, int var2);void setAttribute(String var1, Object var2, int var3);void removeAttribute(String var1, int var2);
关于springBoot中怎么自定义异常响应就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
内容
写法
客户
客户端
文章
更多
浏览器
版本
知识
篇文章
浏览
不同
不错
相同
数据
方法
结果
质量
错误
参考
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
家庭网络安全管理制度
网络安全限制软件
优化数据库表的结构
广东餐饮软件开发公司
我的世界国际版服务器封连点吗
服务器关闭445端口
自己架设服务器对公
华为通信设备软件开发
购买的软件开发费怎么入账
龙吉网络技术有限公司
勤哲服务器通过wps打不开
漫画学数据库
网络安全地域分析
应用服务器品牌型号
宾馆服务类型数据库
数据库错误代码170
数据库中真值用什么数字
数据库找工作怎么找
数控和计算机网络技术哪个更好
珠海 网络安全 公司
安宁软件开发网上价格
dede数据库不显示验证码
软件开发项目成本统计
网络安全论文450
漳州市网络安全知识
内蒙古软件开发厂家现货
网络安全检测系统论文
数据库如何查询多个手机号
数据库基本理论
现代数据库理解