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. GitLab non-standard SSH port

    /***************************************************************************** * GitLab non-standard ...

  2. 【剑指offer】数组中的逆序对,C++实现

    原创博文,转载请注明出处!本题牛客网地址 博客文章索引地址 博客文章中代码的github地址 1.题目 2.思路 3.代码 class Solution { public: int InversePa ...

  3. HDU - 5728:PowMod (欧拉函数&指数循环节)

    Declare: k=∑ m i=1 φ(i∗n) mod 1000000007 k=∑i=1mφ(i∗n) mod 1000000007 n n is a square-free number. φ ...

  4. 数据库中字段的数据类型与JAVA中数据类型的对应关系

    类型名称 显示长度 数据库类型 JAVA类型 JDBC类型索引(int) 描述             VARCHAR L+N VARCHAR java.lang.String 12   CHAR N ...

  5. 设置正确的DNS

    上网或下载软件慢问题,除去少数设备陈旧.感染病毒.网卡硬件故障和网卡驱动错误方面的因素,绝大部分原因是由于部分上网电脑或家用无线路由器设置了错误的DNS造成的,正确的方法是应该在电脑或路由器上使用本地 ...

  6. #51单片机#8位数码管(74HC595芯片)的使用方法

    数码管基本属性:1.采用2片595驱动数码管,需要单片机3路IO口,根据数码管动态扫描原理进行显示:2.宽工作电压3.3V到5V:3.PCB板尺寸:71mm*22mm4.数码管型号:0.36 4位共阳 ...

  7. 管理11gRAC基本命令 (转载) 很详细

    在 Oracle Clusterware 11g 第 2 版 (11.2) 中,有许多子程序和命令已不再使用:    crs_stat    crs_register    crs_unregiste ...

  8. MVVM模式下 修改 store的ajax请求url。

    MVVM模式下 修改 store的ajax请求url. view.down('Pro').getViewModel().getStore('xx_store').proxy.url = "s ...

  9. java代码List接口和Arraylist类

    总结: package clientFrame; import java.util.*; //集合类 List是一个接口.ArrayList是一个类 public class ArraylitTest ...

  10. mina中责任链模式的实现

    一.mina的框架回顾 责任链模式在mina中有重要的作用,其中Filter机制就是基于责任链实现的. 从上图看到消息的接受从IoService层先经过Filter层过滤处理后最后交给IoHander ...