一、错误处理方法

1.Spring Boot 将所有的错误默认映射到/error, 实现ErrorController

2.添加自定义的错误页面

 

二、Spring Boot 将所有的错误默认映射到/error, 实现ErrorController

  2.1  编写ErrorController

package com.shyroke.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping(value="/error")
public class ErrController implements ErrorController { private static final Logger logger = LoggerFactory.getLogger(ErrController.class); @Override
public String getErrorPath() {
logger.info("出错啦!进入自定义错误控制器");
return "error/error";
} @RequestMapping
public String error() {
return getErrorPath();
} }

  2.2  编写错误页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
错误啦,请联系管理员!!! </body>
</html>

  2.3  application.properties

#主配置文件,配置了这个会优先读取里面的属性覆盖主配置文件的属性
spring.profiles.active=dev
server.port=8888 logging.config=classpath:log4j2-dev.xml
spring.mvc.view.prefix: /WEB-INF/templates/
spring.mvc.view.suffix: .jsp

  2.4  结果

三、添加自定义的错误页面

  • html静态页面:在resources/public/error/ 下定义

如添加404页面: resources/public/error/404.html页面,中文注意页面编码

  • 模板引擎页面:在templates/error/下定义

如添加5xx页面: templates/error/5xx.ftl

  • 注:templates/error/ 这个的优先级比较 resources/public/error/高

(九)SpringBoot之错误处理的更多相关文章

  1. 《调试九法——软硬件错误的排查之道》【PDF】下载

    <调试九法--软硬件错误的排查之道>[PDF]下载链接: https://u253469.ctfile.com/fs/253469-231196352 内容简介 <调试九法:软硬件错 ...

  2. SpringBoot自定义错误信息,SpringBoot适配Ajax请求

    SpringBoot自定义错误信息,SpringBoot自定义异常处理类, SpringBoot异常结果处理适配页面及Ajax请求, SpringBoot适配Ajax请求 ============== ...

  3. SpringBoot自定义错误页面,SpringBoot 404、500错误提示页面

    SpringBoot自定义错误页面,SpringBoot 404.500错误提示页面 SpringBoot 4xx.html.5xx.html错误提示页面 ====================== ...

  4. springboot自定义错误页面

    springboot自定义错误页面 1.加入配置: @Bean public EmbeddedServletContainerCustomizer containerCustomizer() { re ...

  5. 【链接】SpringBoot启动错误

    [错误解决]SpringBoot启动错误 https://blog.csdn.net/Small_Mouse0/article/details/78551900

  6. Springboot 的错误处理功能的实现

    一.页面的形式返回 直接在resources的目录下创建public/error的路径,然后创建5xx.html或者4xx.html文件,如果出错就会自动跳转的相应的页面. 二.cotroller的形 ...

  7. 关于Springboot打包错误的问题 | Failed to execute goal org.springframework.boot:spring-boot-maven-plugin

    最近在使用spring-boot整合多模块,但是在父pom中打包maven install时总会报错:Failed to execute goal org.springframework.boot:s ...

  8. SpringBoot定制错误页面

    (1)有模板引擎的情况下,例如404错误,将会在thymeleaf的templates的error下寻找404.html,如果找不到再寻找4xx.html *所有4开头的错误状态码如果找不到特定的ht ...

  9. springboot系列六、springboot配置错误页面及全局异常

    一.spring1.x中处理方式 @Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return new ...

随机推荐

  1. jquery判断一个标签是否包含另外一个标签

    jquery判断一个标签是否包含另外一个标签 一.总结 一句话总结: jquery的find方法配合length属性:if($("#video_md_9_1").find(&quo ...

  2. 监控、日志、APM整个监控体系思考 我为峰2014 简书作者 4.6092018-11-19 11:39打开App 序言

    监控.日志.APM整个监控体系思考 我为峰2014 简书作者 4.6092018-11-19 11:39打开App 序言

  3. window.open()详解及浏览器兼容性问题示例探讨

    这篇文章主要介绍了window.open()的使用及浏览器兼容性问题方面的知识,感兴趣的朋友可以参考下   一.基本语法: window.open(pageURL,name,parameters) 其 ...

  4. 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_02-Eureka注册中心-搭建Eureka单机环境

    我们先搭建单机环境 govern是治理的意思, 这样就把工程创建好了 创建包 创建SpringBoot的启动类. 在父工程里面已经确定了Spring Cloud的版本了.相当于锁定了版本 接下里只需要 ...

  5. Ubuntu 14.04 apache安装配置

    http://jingyan.baidu.com/article/6d704a130c8a0d28da51ca5f.html Ubuntu 14.04 apache安装配置 1.安装 ~# apt-g ...

  6. LeetCode_83. Remove Duplicates from Sorted List

    83. Remove Duplicates from Sorted List Easy Given a sorted linked list, delete all duplicates such t ...

  7. shell脚本安装python、pip--交互式的

    首先把pip-.tgz 安装包放在 /usr/local 下面,按照顺序先安装pip,再安装python.不要先安装或只安装python,否则很容易出错, [root@bogon ~]# cat pi ...

  8. Linq to sql中继承类映射转换问题

    类型为的数据成员“Int32 VTOUID”不是类型的映射的一部分.该成员是否位于继承层次结构根节点的上方? 想躲开Linq to sql中问题限制可真是不容易: http://www.makaido ...

  9. DefaultListBeanFactory的子类之SimpleAliasRegistry

    DefaultListBeanFactory类结构层次图 从继承图看,SimpleAliasRegistry是DefaultListBeanFactory继承类中最底层的实现类. SimpleAlia ...

  10. cisco 各类子网的划分

    C类地址子网划分 /25 子网掩码为128: 1位的取值为1,其他7位的取值为0(10000000): 块大小128: (256-128): 2个子网,每个子网最多包含126台主机: /26 子网掩码 ...