springboot - 使用ErrorAttributes 在我们自定义的 ErrorController中
1、概览

基于《springboot - 映射 /error 到自定义且实现了ErrorController的Controller》改造,仅将MyCustomErrorController类修改如下:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.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.ServletWebRequest; import javax.servlet.http.HttpServletRequest;
import java.util.Map; /**
* @author www.gomepay.com
* @date 2019/11/18
*/
@Controller
public class MyCustomErrorController implements ErrorController {
@Autowired
private ErrorAttributes errorAttributes; @RequestMapping("/error")
@ResponseBody
public String handleError(HttpServletRequest request) {
ServletWebRequest servletWebRequest = new ServletWebRequest(request);
Map<String, Object> errorAttributes = this.errorAttributes.getErrorAttributes(servletWebRequest, true);
final StringBuilder errorDetails = new StringBuilder();
errorAttributes.forEach((attribute, value) -> {
errorDetails.append("<tr><td>")
.append(attribute)
.append("</td><td><pre>")
.append(value)
.append("</pre></td></tr>");
}); return String.format("<html><head><style>td{vertical-align:top;border:solid 1px #666;}</style>"
+ "</head><body><h2>Error Page</h2><table>%s</table></body></html>", errorDetails.toString());
} @Override
public String getErrorPath() {
return "/error";
}
}
执行:


在我们自定义的 ErrorController中使用ErrorAttributes,可代替error.jsp 404.jsp页面。
springboot - 使用ErrorAttributes 在我们自定义的 ErrorController中的更多相关文章
- springboot - 返回xml error 从自定义的 ErrorController
1.概览 2.在<springboot - 返回JSON error 从自定义的 ErrorController>基础上,做如下调整: 1).新增Attribute类和Error类 pac ...
- springboot - 返回JSON error 从自定义的 ErrorController
使用AbstractErrorController(是ErrorController的实现),返回json error. 1.概览 2.基于<springboot - 映射 /error 到自定 ...
- SpringBoot入门(五)——自定义配置
本文来自网易云社区 大部分比萨店也提供某种形式的自动配置.你可以点荤比萨.素比萨.香辣意大利比萨,或者是自动配置比萨中的极品--至尊比萨.在下单时,你并没有指定具体的辅料,你所点的比萨种类决定了所用的 ...
- springboot + 拦截器 + 注解 实现自定义权限验证
springboot + 拦截器 + 注解 实现自定义权限验证最近用到一种前端模板技术:jtwig,在权限控制上没有用springSecurity.因此用拦截器和注解结合实现了权限控制. 1.1 定义 ...
- 不使用SpringBoot如何将原生Feign集成到Spring中来简化http调用
在微服务架构中,如果使用得是SpringCloud,那么只需要集成SpringFeign就可以了,SpringFeign可以很友好的帮我们进行服务请求,对象解析等工作. 然而SpingCloud是依赖 ...
- 自定义的ViewGroup中添加自定义View 造成的无法显示问题(个人)
首先说一下我在网上找了很久没有找到说明方法所以我就自己试着写了一下 1.我自定义了一个继承了RelativeLayout的ViewGroup java代码如下: /** * 简单自定义三头像 * @a ...
- android中自定义的dialog中的EditText无法弹出输入法解决方案
1.解决无法弹出输入法: 在show()方法调用之前,用dialog.setView(new EditText(context))添加一个空的EditText,由于是自定义的AlertDialog,有 ...
- 【 Android】自定义的AlertDialog中的EditText无法调用输入法问题解决
1.问题描述: 在自定义的AlertDialog 中添加了EditText组件,但运行时怎么点EditText都无法调出软键盘: 2.原因分析: 一开始我以为EditText的focus属性没有设置好 ...
- Dynamics 365 CE的插件/自定义工作流活动中调用Web API示例代码
微软动态CRM专家罗勇 ,回复325或者20190428可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me! 现在Web API越来越流行,有时候为了程序更加健壮,需要在插件 ...
随机推荐
- leetcode LRU缓存机制(list+unordered_map)详细解析
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - 如果密钥 (key) 存 ...
- hadoop常用的操作指令
-help:查看帮助 hadoop fs -help rm -rm [-f] [-r|-R] [-skipTrash] <src> ... : Delete all files that ...
- java、mysql、oracle、pgsql数据类型对应关系
看不清 请 Ctrl+鼠标滚轮 放大页面
- Java HotSpot(TM) Client VM 与 server VM 的配置
在Linux 6.5 下安装Elasticsearch 出现错误: JVM is using the client VM [Java HotSpot(TM) Client VM] but should ...
- [Verilog] indexed part-select +:
That syntax is called an indexed part-select. The first term is the bit offset and the second term ...
- 嵊州普及Day5T1
题意:有n个商店,自家商店的定价不可高于任何一家商店定价,求自家商店最高定价. 思路:拿个变量打擂台即可,不用解释太多. 见代码: #include<iostream> #include& ...
- 单元测试框架TestNg使用总结
工欲善其事,必先利其器 单元测试的重要性是不言而喻的.但如果没有好的单元测试工具,是无法激起开发人员的欲望. Testng便是利器之一.TestNG是基于Annotation的测试框架的先驱,他拥有通 ...
- JS实现全选,取消全选,正常选择
//点击选择方法 onUserIdsChange(selVal) { if (this.form.groupUserIds.includes(-1) && !this.isSelect ...
- CSP-J/S2019试题选做
S D1T2 括号树 设\(f[u]\)表示根到\(u\)的路径上有多少子串是合法括号串.(即题目里的\(k_u\),此变量名缺乏个性,故换之) 从根向每个节点dfs,容易求出\(c[u]\):表示从 ...
- SSM+Redis结构框架及概述
1.Spring IoC承担了一个资源管理.整合.即拔即插的功能. 2.Spring AOP可以提供切面管理,特别是数据库事务管理的功能. 3.Spring MVC用于把模型.视图和控制器分层,组成一 ...