springboot - 映射 HTTP Response Status Codes 到自定义 JSP Error 页面
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> <dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency> </dependencies>
2)、application.properties
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
server.error.whitelabel.enabled=false
server.error.include-stacktrace=always
3)、java代码
package com.ebc.controller; import com.ebc.error.ForbiddenException;
import com.ebc.error.NotYetImplemented;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus; @Controller
public class MyController {
/**
* response status code=500,导航到5xx.jsp
*/
@RequestMapping("/")
public void handleRequest() {
throw new RuntimeException("test exception");
}
/**
* response status code=501,导航到5xx.jsp
*/
@RequestMapping("/app")
public void handleAppInfoRequest() throws NotYetImplemented {
throw new NotYetImplemented("The request page is not yet implemented");
} /**
* response status code=403,因为没有403.jsp,因此会导航到error.jsp
*/
@RequestMapping("/admin")
public void handleAdminRequest() throws ForbiddenException {
throw new ForbiddenException("The requested page is forbidden");
} /**
* 返回501,但是无法导航到5xx.jsp页面
*/
@RequestMapping("/some")
@ResponseStatus(HttpStatus.NOT_IMPLEMENTED)
public void handleSomeRequest() {
}
/**
* 返回501,但是无法导航到5xx.jsp页面
*/
@RequestMapping("/some2")
public ResponseEntity<?> handleSomeInfoRequest() {
ResponseEntity<?> re = new ResponseEntity<>((Object)null, HttpStatus.NOT_IMPLEMENTED);
return re;
}
} package com.ebc.error; import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus; @ResponseStatus(HttpStatus.FORBIDDEN)
public class ForbiddenException extends Exception {
public ForbiddenException(String message) {
super(message);
}
} package com.ebc.error; import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus; @ResponseStatus(HttpStatus.NOT_IMPLEMENTED)
public class NotYetImplemented extends Exception {
public NotYetImplemented(String message) {
super(message);
}
}
4)、5xx.jsp
所有5开头的http状态返回码都会导航到该5xx.jsp。
<%@ page language="java"
contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<style>
table td{
vertical-align:top;
border:solid 1px #888;
padding:10px;
}
</style>
</head>
<body>
<h1>My 5xx Error Page</h1>
<table>
<tr>
<td>Date</td>
<td>${timestamp}</td>
</tr>
<tr>
<td>Error</td>
<td>${error}</td>
</tr>
<tr>
<td>Status</td>
<td>${status}</td>
</tr>
<tr>
<td>Message</td>
<td>${message}</td>
</tr>
<tr>
<td>Exception</td>
<td>${exception}</td>
</tr>
<tr>
<td>Trace</td>
<td>
<pre>${trace}</pre>
</td>
</tr>
</table>
</body>
</html>
5)、404.jsp
只有404的返回码会导航到404.jsp
<%@ page language="java"
contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<style>
table td{
vertical-align:top;
border:solid 1px #888;
padding:10px;
}
</style>
</head>
<body>
<h1>My 404 Error Page</h1>
<table>
<tr>
<td>Date</td>
<td>${timestamp}</td>
</tr>
<tr>
<td>Error</td>
<td>${error}</td>
</tr>
<tr>
<td>Status</td>
<td>${status}</td>
</tr>
<tr>
<td>Message</td>
<td>${message}</td>
</tr>
<tr>
<td>Exception</td>
<td>${exception}</td>
</tr>
<tr>
<td>Trace</td>
<td>
<pre>${trace}</pre>
</td>
</tr>
</table>
</body>
</html>
6)、error.jsp(在views文件夹下,即application.properties 中的spring.mvc.view.prefix=/WEB-INF/views/)
除了5xx、404外的所有状态码都会到航到error.jsp页面
<%@ page language="java"
contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<style>
table td{
vertical-align:top;
border:solid 1px #888;
padding:10px;
}
</style>
</head>
<body>
<h1>My Custom Global Error Page</h1>
<table>
<tr>
<td>Date</td>
<td>${timestamp}</td>
</tr>
<tr>
<td>Error</td>
<td>${error}</td>
</tr>
<tr>
<td>Status</td>
<td>${status}</td>
</tr>
<tr>
<td>Message</td>
<td>${message}</td>
</tr>
<tr>
<td>Exception</td>
<td>${exception}</td>
</tr>
<tr>
<td>Trace</td>
<td>
<pre>${trace}</pre>
</td>
</tr>
</table>
</body>
</html>
3、执行




发现,没有导航到5xx.jsp,只是返回了501

输入一个不存在的资源,导航到了404.jsp了。
总结:
views
|_error.jsp【error.jsp一定存放在根下,即application.properties 中的spring.mvc.view.prefix=/WEB-INF/views/】
|_error【其他错误页面一定存放在error目录下】
|_5xx.jsp
|_404.jsp
springboot - 映射 HTTP Response Status Codes 到自定义 JSP Error 页面的更多相关文章
- springboot - 映射HTTP Response Status Codes 到 静态 HTML页面
1.总览 2.代码 1).pom.xml <dependencies> <dependency> <groupId>org.springframework.boot ...
- springboot - 映射HTTP Response Status Codes 到 FreeMarker Error页面
1.总览 2.代码 1).pom.xml 这里注意:springboot 2.2.0以后默认的freemarker文件后缀为:ftlh.本例用的是2.2.1,所以后缀为ftlh <depende ...
- 【SpringBoot】SpringBoot拦截器实战和 Servlet3.0自定义Filter、Listener
=================6.SpringBoot拦截器实战和 Servlet3.0自定义Filter.Listener ============ 1.深入SpringBoot2.x过滤器Fi ...
- SpringBoot整合WEB开发--(五)自定义错误页
目的与原理: 处理异常时,若我们想根据实际情况返回不同的页面,@ControllerAdvice与@ExceptionHandler,一般用于处理应用级别的异常,一些容器级别的错误就处理不了,例如Fi ...
- HTTP状态码(HTTP Status codes)简介
HTTP可能大家都熟悉,就是超文本传输协议.浏览器通过HTTP与WEB Server通讯(也有一些其它软件比如IM使用HTTP协议传递数据),把我们的请求(HTTP Request)传递给服务器,服务 ...
- HTTP response status
The status code is a 3-digit number: 1xx (Informational): Request received, server is continuing the ...
- C#、JAVA操作Hadoop(HDFS、Map/Reduce)真实过程概述。组件、源码下载。无法解决:Response status code does not indicate success: 500。
一.Hadoop环境配置概述 三台虚拟机,操作系统为:Ubuntu 16.04. Hadoop版本:2.7.2 NameNode:192.168.72.132 DataNode:192.168.72. ...
- IIS SMTP status codes
Here are the meaning of SMTP status codes. Status Code Description 211 System status, or system help ...
- HTTP常见返回代码(HTTP Status codes)的分类和含义
HTTP错误主要分成三类:用户设备问题.Web服务器问题和连接问题.当客户端向Web服务器发送一个HTTP请求时,服务器都会返回一个响应代码.而这些响应代码主要分成五类. HTTP状态码中定义了5大类 ...
随机推荐
- 如何更改linux(centos)下的Apache http端口号
# vi /etc/httpd/conf/httpd.conf 文件 修改两个地方 #Listen 12.34.56.78:80 Listen 80 #把80改为你设置的端 ...
- Laradock 使用中遇到的问题汇总
1.ErrorException] mkdir (): Permission denied 解决:权限不够,thinkphp5下,runtime 文件夹改777,文件所有者改为 laradock(进入 ...
- 新闻网大数据实时分析可视化系统项目——5、Hadoop2.X HA架构与部署
1.HDFS-HA架构原理介绍 hadoop2.x之后,Clouera提出了QJM/Qurom Journal Manager,这是一个基于Paxos算法实现的HDFS HA方案,它给出了一种较好的解 ...
- Lamda简单使用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 51nod 1380:夹克老爷的逢三抽一
1380 夹克老爷的逢三抽一 基准时间限制:1 秒 空间限制:131072 KB 分值: 320 难度:7级算法题 收藏 取消关注 又到了诺德县的百姓孝敬夹克大老爷的日子,带着数量不等的铜板的村民 ...
- eclipse springboot运行helloworld错误: 找不到或无法加载主类 xxx.xxx.xxx
这个错误,在网上搜找了好久,说是什么jar包冲突,什么环境配置,我经过验证均是正确的,javac java java -version 都没问题,环境变量也OK,各种解释均没有能够解决我的问题,最后好 ...
- upper_bound()和low_bound函数的基本使用和理解(转载,已获博主授权)
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sdz20172133/article/details/80101838 前提:一个非降序列!!!!! ...
- leetcode1019 Next Greater Node In Linked List
""" We are given a linked list with head as the first node. Let's number the nodes in ...
- DevOps - 生命周期
章节 DevOps – 为什么 DevOps – 与传统方式区别 DevOps – 优势 DevOps – 不适用 DevOps – 生命周期 DevOps – 与敏捷方法区别 DevOps – 实施 ...
- CentOS 6.5(x86_32)下安装Oracle 10g R2
一.硬件要求 1.内存 & swap Minimum: 1 GB of RAMRecommended: 2 GB of RAM or more 检查内存情况 # grep MemTotal / ...