1 BasicErrorController

  1.1 简述

    SpringMVC框架在出现错误时有一个默认的错误请求 /error;出现异常之后执行/error请求之前框架会判断出现异常的请求类型,然后根据请求类型判断是返回一个HTML页面还是JSON格式的错误信息

  1.2 源码分析

    BasicErrorController主要处理 /error 请求,该类中有两个处理 /error 请求的方法,分别是 errorHtml 和 error,前者返回的是HTML页面后者返回的是JSON格式的数据

    技巧01:如果是浏览器发出的请求出现异常后跳转到 /error 就会执行 errorHtml,然后返回一个HTML页面

    技巧02:如果是其他客户端发出请求出现异常后跳转到 /error 就会执行 error ,然后返回一个JSON格式数据

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package org.springframework.boot.autoconfigure.web.servlet.error; import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeStacktrace;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping({"${server.error.path:${error.path:/error}}"})
public class BasicErrorController extends AbstractErrorController {
private final ErrorProperties errorProperties; public BasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties) {
this(errorAttributes, errorProperties, Collections.emptyList());
} public BasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties, List<ErrorViewResolver> errorViewResolvers) {
super(errorAttributes, errorViewResolvers);
Assert.notNull(errorProperties, "ErrorProperties must not be null");
this.errorProperties = errorProperties;
} public String getErrorPath() {
return this.errorProperties.getPath();
} @RequestMapping(
produces = {"text/html"}
)
public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
HttpStatus status = this.getStatus(request);
Map<String, Object> model = Collections.unmodifiableMap(this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.TEXT_HTML)));
response.setStatus(status.value());
ModelAndView modelAndView = this.resolveErrorView(request, response, status, model);
return modelAndView != null ? modelAndView : new ModelAndView("error", model);
} @RequestMapping
@ResponseBody
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
Map<String, Object> body = this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.ALL));
HttpStatus status = this.getStatus(request);
return new ResponseEntity(body, status);
} protected boolean isIncludeStackTrace(HttpServletRequest request, MediaType produces) {
IncludeStacktrace include = this.getErrorProperties().getIncludeStacktrace();
if (include == IncludeStacktrace.ALWAYS) {
return true;
} else {
return include == IncludeStacktrace.ON_TRACE_PARAM ? this.getTraceParameter(request) : false;
}
} protected ErrorProperties getErrorProperties() {
return this.errorProperties;
}
}

BasicErrorController.java

  1.3 实例

    1.3.1 浏览器

      利用浏览器请求一个不存在的url

        

    1.3.2 postman

      利用postman请求一个不存在的url

        

  1.4 修改默认的HTML页面

    如果是浏览器发送的请求出现异常后,跳转到 /error 请求后会根据错误编号返回对应的HTML文档,例如:404.html、500.html

    技巧01:我们可以重写这些404.html等文档,直接在 src/main/resources 路径下创建一个 resources/error 文件夹,形如:

      

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>你所访问的资源不存在</h2>
</body>
</html>

404.html

    技巧02:在到 error 文件夹内部创建 404.html 等异常HTML文件即可,重启项目后利用浏览器访问一个不存在的url就会返回自定义的那个404.html文档,例如:

      

/error处理的更多相关文章

  1. Mediaplayer error (-19,0)

    Android MediaPlayer 发生 error (-19,0) 错误解决方法. 引起原因:由于多次实例化MediaPlayer.start() 进行播放操作引起的.由于没有及时释放内存资源导 ...

  2. 4.Android 打包时出现的Android Export aborted because fatal error were founds [closed]

    Android 程序开发完成后,如果要发布到互联网上供别人使用,就需要将自己的程序打包成Android 安装包文件(Android Package,APK),其扩展名为.apk.使用run as 也能 ...

  3. myeclipse 内存不够用报错PermGen space 和 An internal error has occurred.

    最近项目中又增加了新的模块,项目的代码又多了不少.运行的时候总是报如下错误 Exception in thread "http-apr-80-exec-6" java.lang.O ...

  4. error C4430:missing type specifier 解决错误

    错误    3    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int ...

  5. PhpStorm和WAMP配置调试参数,问题描述Error. Interpreter is not specified or invalid. Press “Fix” to edit your project configuration.

    PhpStorm和WAMP配置调试参数 问题描述: Error. Interpreter is not specified or invalid. Press “Fix” to edit your p ...

  6. Visual Studio:error MSB8020(搬运)

    状况如下: error MSB8020: The builds tools for v120 (Platform Toolset = 'v120') cannot be found. To build ...

  7. 转:ORA-15186: ASMLIB error function = [asm_open], error = [1], 2009-05-24 13:57:38

    转:ORA-15186: ASMLIB error function = [asm_open], error = [1], 2009-05-24 13:57:38http://space.itpub. ...

  8. 解决 Error: getaddrinfo EADDRINFO 错误

    安装npm失败,提示Error: getaddrinfo EADDRINFO,原因在于虚拟机未连接互联网,悲剧.

  9. mono-3.4.0 源码安装时出现的问题 [do-install] Error 2 [install-pcl-targets] Error 1 解决方法

    Mono 3.4修复了很多bug,继续加强稳定性和性能(其实Mono 3.2.8 已经很稳定,性能也很好了),但是从http://download.mono-project.com/sources/m ...

  10. keil MDK error: L6236E: No section matches selector - no section 错误

    今天板子刚到,新建的第一个工程就报错了. .\Objects\cse.sct(7): error: L6236E: No section matches selector - no section t ...

随机推荐

  1. [分享]Google 全球 IP 地址库[Google Global Cache IPs]

    Google 全球 IP 地址库 IP 地址来源:http://www.kookle.co.nr,共计4351个. Bulgaria 93.123.23.1 93.123.23.2 93.123.23 ...

  2. Spring字符编码过滤器

    1 <filter> 2 <filter-name>characterEncodingFilter</filter-name> 3 <filter-class ...

  3. 【retina】手机上 1PX 边框

    1像素边框,在2倍屏幕上为2“占位”,3倍屏上为3“占位”,但设计师就要1“占位” .content h1:after, .content h2:after { border-top: 1px sol ...

  4. 如何制作CDLinux启动盘

    用笔记本安装虚拟机并且尝试使用CDLinux系统进行无线密码破解的朋友们可能会遇到很多的问题,今天的经验就是总结了很多的失败然后整理出来的,希望能够对大家有所帮助.本次经验来和大家分享一下使用U盘制作 ...

  5. 剑指offer-第四章解决面试题的思路(包含min函数的栈)

    题目:定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min函数,在该栈中,调用min,push及pop的时间复杂度都是O(1) 思路:定义两个栈分别为dataStack和minStack ...

  6. spring源码学习之:项目公共配置项解决方案

    一:项目中有一些key,value的简单配置 org.apache.commons.configuration.DatabaseConfiguration可以轻松解决 二:配置项目的xml中bean ...

  7. Aix之 xmanager 2.0连接AIX服务器

    xmanager连接AIX服务器可以分为两种情况:1.连接IBM服务器,使用远程桌面功能进行系统维护.要求这台服务器已经安装了图形桌面,如CDE等,并启动到图形界面.在xmanager中的Xbrows ...

  8. HTTP API 设计指南

    本指南描述了一系列 HTTP+JSON API 的设计实践, 来自并展开于 Heroku Platform API 的工作.本指南指导着Heroku内部API的开发,我们希望也能对Heroku以外的A ...

  9. 借助curl理解$GLOBALS['HTTP_RAW_POST_DATA'] ,$_POST, php://input

    发送请求代码 post.php <?php $url='http://localhost/web/curl/url.php'; $data='a=123|b=2&c=3'; $heade ...

  10. java数组复制===clone()

    总结:使用方法原理弄清楚 package com.a; public class gjsopb { public static void main(String[] args) { int a[] = ...