1、总览

2、代码

1)、pom.xml

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> </dependencies>

2)、application.properties

server.error.whitelabel.enabled=false
server.error.include-stacktrace=always

3)、MyController.java

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class MyController {
@RequestMapping("/")
public String handler (Model model) {
throw new RuntimeException("test exception");
}
}

4)、MyCustomErrorController.java

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 javax.servlet.http.HttpServletRequest; /**
* @author www.gomepay.com
* @date 2019/11/18
*/
@Controller
public class MyCustomErrorController implements ErrorController { @RequestMapping("/error")
@ResponseBody
public String handleError(HttpServletRequest request) {
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
Exception exception = (Exception) request.getAttribute("javax.servlet.error.exception");
return String.format("<html><body><h2>Error Page</h2><div>Status code: <b>%s</b></div>"
+ "<div>Exception Message: <b>%s</b></div><body></html>",
statusCode, exception==null? "N/A": exception.getMessage());
} @Override
public String getErrorPath() {
return "/error";
}
}

3、执行

最新文章

  1. java反射复制属性值
  2. psutil 模块
  3. ACM-JAVA基础
  4. NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
  5. Cardinal:一个用于移动项目开发的轻量 CSS 框架
  6. PreferenceScreen监听子项的刷新
  7. 开发板ping不通主机和虚拟机的看过来(转载)!
  8. 用NULL布局为什么不能显示
  9. linux-6的yum软件仓库
  10. JavaScript数字精度上代码。
  11. POJ C++程序设计 编程题#1 编程作业—多态与虚函数
  12. java基础(十三)常用类总结(三)
  13. Intel baytrail-t support Linux?
  14. ***1133. Fibonacci Sequence(斐波那契数列,二分,数论)
  15. PHP中require和include的区别
  16. Testlink插件工具
  17. angular6、7 兼容ie9、10、11
  18. odoo开发笔记--模型中常用的方法
  19. 小菜菜mysql练习解读分析2——查询存在&quot; 01 &quot;课程但可能不存在&quot; 02 &quot;课程的情况(不存在时显示为 null )
  20. 基于express + express-myconnection 的请求处理模板

热门文章

  1. 1-4SpringBoot操作之Spring-Data-Jpa(一)
  2. javascript 在页面不刷新的情况下 其中的变量时不会被初始化的
  3. Ubuntu 移植 ffmpeg + x264
  4. 第1节 IMPALA:1、impala的基本介绍
  5. delphi环境变量
  6. Python 3.8 新功能【新手必学】
  7. Redis详解(六)——哨兵机制
  8. apache端口修改为80
  9. Ceph 概念理解
  10. springboot指定配置文件运行