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