springboot - 映射HTTP Response Status Codes 到 静态 HTML页面
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)、MyController.java
package com.ebc.controller; import com.ebc.error.NotYetImplemented;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class MyController {
/**
* response status code=500,导航到5xx.html
*/
@RequestMapping("/")
public void handleRequest() {
throw new RuntimeException("test exception");
}
/**
* response status code=501,导航到5xx.html
*/
@RequestMapping("/app")
public void handleAppInfoRequest() throws NotYetImplemented {
throw new NotYetImplemented("The request page is not yet implemented");
} }
3)、ForbiddenException.java
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);
}
}
4)、NotYetImplemented.java
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);
}
}
5)、5xx.html
<html>
<body>
<h1>My 5xx Error Page</h1>
<div id="locationDiv"></div>
<script>
document.getElementById("locationDiv").innerHTML = "Location: " + window.location.href;
</script>
</body>
</html>
6)、404.html
<html>
<body>
<h1>My 404 Error Page</h1>
<div id="locationDiv"></div>
<script>
document.getElementById("locationDiv").innerHTML = "Location: " + window.location.href;
</script></body>
</html>
3、执行



总结:
1、如果出现了非5xx、404返回码如403时,怎么办?使用error.jsp,并将其放在根下,如本例需放在static目录下。
2、5xx.html、404.html需放在error目录下。
springboot - 映射HTTP Response Status Codes 到 静态 HTML页面的更多相关文章
- springboot - 映射HTTP Response Status Codes 到 FreeMarker Error页面
1.总览 2.代码 1).pom.xml 这里注意:springboot 2.2.0以后默认的freemarker文件后缀为:ftlh.本例用的是2.2.1,所以后缀为ftlh <depende ...
- springboot - 映射 HTTP Response Status Codes 到自定义 JSP Error 页面
1.总览 2.代码 1).pom.xml <dependencies> <dependency> <groupId>org.springframework.boot ...
- HTTP状态码(HTTP Status codes)简介
HTTP可能大家都熟悉,就是超文本传输协议.浏览器通过HTTP与WEB Server通讯(也有一些其它软件比如IM使用HTTP协议传递数据),把我们的请求(HTTP Request)传递给服务器,服务 ...
- SpringBoot学习笔记(3):静态资源处理
SpringBoot学习笔记(3):静态资源处理 在web开发中,静态资源的访问是必不可少的,如:Html.图片.js.css 等资源的访问. Spring Boot 对静态资源访问提供了很好的支持, ...
- 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大类 ...
- returned a response status of 403 OR 409
当我们使用jersy把图片上传到我们的图片服务器中[tomcat],我们可能会有以下的错误: returned a response status of 403 OR 409 403和409我都遇到过 ...
随机推荐
- Day9 - F - Monkey and Banana HDU - 1069
一组研究人员正在设计一项实验,以测试猴子的智商.他们将挂香蕉在建筑物的屋顶,同时,提供一些砖块给这些猴子.如果猴子足够聪明,它应当能够通过合理的放置一些砖块建立一个塔,并爬上去吃他们最喜欢的香蕉. ...
- python编写banner获取的常用模块
模块的概念:模块也叫库,每个模块中都内置了大量的功能和函数.类和变量.它就像是积木,可以根据需要进行调用组合.模块就是程序,每个模块就是一个后缀为.py的Python程序.Python的模块分为标准模 ...
- 编程题目: 两个队列实现栈(Python)
感觉两个队列实现栈 比 两个栈实现队列 麻烦 1.栈为空:当两个队列都为空的时候,栈为空 2.入栈操作:当队列2为空的时候,将元素入队到队列1:当队列1位空的时候,将元素入队到队列2: 如果队列1 和 ...
- C++中的简单继承
Father.cpp: #include<iostream> using namespace std; class Father { protected: int width; int h ...
- TP5分页函数paginate中的each()传参
在使用each()时,each()里面只能使用局部变量,当使用外部变量时会报未定义变量的错误,但是有时候我们还必须使用外部变量,就需要想是时作用域的问题,但是如果使用 global 全局虽然不报错,但 ...
- SpringBoot配置介绍
SpringBoot配置介绍 SpringBoot如何进行配置 在SpringBoot中默认使用Servlet3.0可以没有web.xml,没有任何的xml,我们想要做一些自定义配置,比u数据库相关信 ...
- 小程序之scroll-view用法 - 水平滚动
<scroll-view class="box" scroll-x="true" > <view class="box-item&q ...
- Jenkins实现自动打包,MAVEN打包,Shell脚本启动
1.点击New任务 2.创建任务,输入项目名 3.输入描述等 4.选择Git或SVN 5.自动,定时打包 6.在Build下配置
- Linux 下配置多路径及SCSI扫描磁盘重新发现大小
Linux SCSI扫描磁盘重新发现大小: # for i in `find /sys/class/scsi_host/host*`; do echo 1 >> $i/issue_lip; ...
- 7 —— node —— 响应图片
const http = require('http'); const fs = require('fs'); const server = http.createServer(); server ...