1、总览

2、代码

1)、pom.xml

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6.  
  7. <dependency>
  8. <groupId>org.projectlombok</groupId>
  9. <artifactId>lombok</artifactId>
  10. <optional>true</optional>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-tomcat</artifactId>
  15. <scope>provided</scope>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-test</artifactId>
  20. <scope>test</scope>
  21. </dependency>
  22.  
  23. </dependencies>

2)、MyController.java

  1. package com.ebc.controller;
  2.  
  3. import com.ebc.error.NotYetImplemented;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6.  
  7. @Controller
  8. public class MyController {
  9. /**
  10. * response status code=500,导航到5xx.html
  11. */
  12. @RequestMapping("/")
  13. public void handleRequest() {
  14. throw new RuntimeException("test exception");
  15. }
  16. /**
  17. * response status code=501,导航到5xx.html
  18. */
  19. @RequestMapping("/app")
  20. public void handleAppInfoRequest() throws NotYetImplemented {
  21. throw new NotYetImplemented("The request page is not yet implemented");
  22. }
  23.  
  24. }

3)、ForbiddenException.java

  1. package com.ebc.error;
  2.  
  3. import org.springframework.http.HttpStatus;
  4. import org.springframework.web.bind.annotation.ResponseStatus;
  5.  
  6. @ResponseStatus(HttpStatus.FORBIDDEN)
  7. public class ForbiddenException extends Exception {
  8. public ForbiddenException(String message) {
  9. super(message);
  10. }
  11. }

4)、NotYetImplemented.java

  1. package com.ebc.error;
  2.  
  3. import org.springframework.http.HttpStatus;
  4. import org.springframework.web.bind.annotation.ResponseStatus;
  5.  
  6. @ResponseStatus(HttpStatus.NOT_IMPLEMENTED)
  7. public class NotYetImplemented extends Exception {
  8. public NotYetImplemented(String message) {
  9. super(message);
  10. }
  11. }

5)、5xx.html

  1. <html>
  2. <body>
  3. <h1>My 5xx Error Page</h1>
  4. <div id="locationDiv"></div>
  5. <script>
  6. document.getElementById("locationDiv").innerHTML = "Location: " + window.location.href;
  7. </script>
  8. </body>
  9. </html>

6)、404.html

  1. <html>
  2. <body>
  3. <h1>My 404 Error Page</h1>
  4. <div id="locationDiv"></div>
  5. <script>
  6. document.getElementById("locationDiv").innerHTML = "Location: " + window.location.href;
  7. </script></body>
  8. </html>

3、执行

总结:

1、如果出现了非5xx、404返回码如403时,怎么办?使用error.jsp,并将其放在根下,如本例需放在static目录下。

2、5xx.html、404.html需放在error目录下。

springboot - 映射HTTP Response Status Codes 到 静态 HTML页面的更多相关文章

  1. springboot - 映射HTTP Response Status Codes 到 FreeMarker Error页面

    1.总览 2.代码 1).pom.xml 这里注意:springboot 2.2.0以后默认的freemarker文件后缀为:ftlh.本例用的是2.2.1,所以后缀为ftlh <depende ...

  2. springboot - 映射 HTTP Response Status Codes 到自定义 JSP Error 页面

    1.总览 2.代码 1).pom.xml <dependencies> <dependency> <groupId>org.springframework.boot ...

  3. HTTP状态码(HTTP Status codes)简介

    HTTP可能大家都熟悉,就是超文本传输协议.浏览器通过HTTP与WEB Server通讯(也有一些其它软件比如IM使用HTTP协议传递数据),把我们的请求(HTTP Request)传递给服务器,服务 ...

  4. SpringBoot学习笔记(3):静态资源处理

    SpringBoot学习笔记(3):静态资源处理 在web开发中,静态资源的访问是必不可少的,如:Html.图片.js.css 等资源的访问. Spring Boot 对静态资源访问提供了很好的支持, ...

  5. HTTP response status

    The status code is a 3-digit number: 1xx (Informational): Request received, server is continuing the ...

  6. 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. ...

  7. IIS SMTP status codes

    Here are the meaning of SMTP status codes. Status Code Description 211 System status, or system help ...

  8. HTTP常见返回代码(HTTP Status codes)的分类和含义

    HTTP错误主要分成三类:用户设备问题.Web服务器问题和连接问题.当客户端向Web服务器发送一个HTTP请求时,服务器都会返回一个响应代码.而这些响应代码主要分成五类. HTTP状态码中定义了5大类 ...

  9. returned a response status of 403 OR 409

    当我们使用jersy把图片上传到我们的图片服务器中[tomcat],我们可能会有以下的错误: returned a response status of 403 OR 409 403和409我都遇到过 ...

随机推荐

  1. Echarts 折线图y轴标签值过长 显示

    参考: https://blog.csdn.net/dandelion_drq/article/details/79270597 改变Y轴单位:https://www.cnblogs.com/cons ...

  2. arm linux 移植 ffmpeg 库 + x264

    背景 Ffmpeg 中带有h264的解码,没有编码,需要添加x264.libx264是一个自由的H.264编码库,是x264项目的一部分,使用广泛,ffmpeg的H.264实现就是用的libx264. ...

  3. windows目标进程注入dll

    在别的程序注入dll 步骤: ,获取目标进程ID,CreateToolhelp32Snapshot()函数; ,获取目标进程句柄,OpenProcess()函数; ,目标进程要一块内存,Virtual ...

  4. Hexo搭建个人博客及next主题基本配置

    前言 国内一些免费的博客平台比如CSDN.博客园都已经很成熟,功能齐全,已经可以满足我们的需求,帮助我们记录学习过程遇到的问题,还能分享帮助其他人解决问题.为什么还要自己动手去搭建博客呢?首先写博客是 ...

  5. push 和 append 以及appendchild 用法和区别

    push() 给数组添加元素,并且返回数组长度 如 : arr.push('a') append() 是jq写法,添加节点到指定父级节点的子节点列表末尾 appendchild() 是append原生 ...

  6. package报错

    java类的第一行package 包路径 package报错 jdk版本不一致 或者jar包冲突

  7. postProcessBeanFactory方法源码跟踪

    看这篇文章之前可以先了解之前的跟踪流程,https://www.jianshu.com/p/4934233f0ead 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 AbstractApplic ...

  8. 清北学堂例题 LUOGU2523【HAOI2011】problem c

    题目描述 给n个人安排座位,先给每个人一个1~n的编号,设第i个人的编号为ai(不同人的编号可以相同),接着从第一个人开始,大家依次入座,第i个人来了以后尝试坐到ai,如果ai被占据了,就尝试ai+1 ...

  9. 04.swoole学习笔记--webSocket服务器

    <?php //创建webSocket服务器 $serv=); //获取请求 //on //open 建立连接 $serv:服务器 $request:客户端信息 $serv->on('op ...

  10. 045、Java中使用if语句进行判断

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...