package com.smartmap.sample.ch1.controller.view;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import com.fasterxml.jackson.databind.ObjectMapper; @Controller
public class ErrorViewController extends AbstractErrorController {
final Log log = LogFactory.getLog(ErrorViewController.class);
ErrorProperties errorProperties; @Autowired
ObjectMapper objectMapper; public ErrorViewController() {
super(new DefaultErrorAttributes());
} public ErrorViewController(ErrorAttributes errorAttributes, ErrorProperties errorProperties,
List<ErrorViewResolver> errorViewResolvers) {
super(errorAttributes, errorViewResolvers);
this.errorProperties = errorProperties;
} @Override
public String getErrorPath() {
return errorProperties.getPath();
} @RequestMapping("/error")
public ModelAndView getErrorInfo(HttpServletRequest request, HttpServletResponse response) { Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(request, false)); Throwable cause = this.getCause(request);
int status = (Integer) model.get("status");
String message = (String) model.get("message");
String errorMessage = getErrorMessage(cause);
log.info(status + "," + message, cause);
response.setStatus(status);
if (!isJsonRequest(request)) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addAllObjects(model);
modelAndView.addObject("errorMessage", errorMessage);
modelAndView.addObject("status", status);
modelAndView.addObject("cause", cause);
return modelAndView;
} else {
Map<String, Object> error = new HashMap();
error.put("errorMessage", errorMessage);
error.put("status", status);
error.put("cause", cause); writeJson(response, error);
return null; }
} protected Throwable getCause(HttpServletRequest request) {
Throwable error = (Throwable) request.getAttribute("javax.servlet.error.exception");
if (error != null) {
while (error instanceof ServletException && error.getCause() != null) {
error = ((ServletException) error).getCause();
}
} return error; } protected void writeJson(HttpServletResponse response, Map error) {
response.setContentType("application/json;charset=utf-8");
try {
response.getWriter().write(objectMapper.writeValueAsString(error));
} catch (IOException e) {
// ignore
}
} protected String getErrorMessage(Throwable ex) {
/* 不给前端显示详细错误 */
return "服务器错误,请联系管理员";
} protected boolean isJsonRequest(HttpServletRequest request) {
return request.getHeader("Accept").contains("application/json");
} }

Spring Boot--01错误处理的更多相关文章

  1. Spring Boot自定义错误页面,Whitelabel Error Page处理方式

    我已经是Spring Framework框架的忠实粉丝.对于企业软件开发者来说它提供了对常见问题的通用解决方案,包括那些你在未来开发中没有意识到的问题.但是,它构建的J2EE项目变得比较臃肿,需要被一 ...

  2. (后端)Spring Boot自定义错误页面,Whitelabel Error Page处理方式(转)

    我已经是Spring Framework框架的忠实粉丝.对于企业软件开发者来说它提供了对常见问题的通用解决方案,包括那些你在未来开发中没有意识到的问题.但是,它构建的J2EE项目变得比较臃肿,需要被一 ...

  3. spring boot 启动错误:Could not resolve placeholder

    在启动整个spring boot项目时,出现错误: Could not resolve placeholder 原因:没有指定好配置文件,因为src/main/resources下有多个配置文件,例如 ...

  4. Spring Boot自定义错误视图

    Spring Boot缺省错误视图解析器 Web应用在处理请求的过程中发生错误是非常常见的情况,SpringBoot中为我们实现了一个错误视图解析器(DefaultErrorViewResolver) ...

  5. Spring boot 启动错误处理:Action: Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. If you have database settings to be loaded from a particular...

    错误原因 在pom中引入了mybatis-spring-boot-starter ,Spring boot默认会加载org.springframework.boot.autoconfigure.jdb ...

  6. Spring Boot → 01:概要

    背景 大约20年前,程序员们使用“企业级Java Bean”(EJB)开发企业应用,需要配置复杂的XML. 在二十世纪初期,新兴Java技术——Spring,横空出世.使用极简XML和POJO(普通J ...

  7. spring boot 之 错误:SpelEvaluationException: EL1008E: Property or field 'timestamp' cannot be found on object of type 'java.util.HashMap'

    这个错误我也见过很多次了,今天终于理解了其出现的原因. 错误是这样的: 2017-11-23 18:05:39.504 ERROR 4092 --- [nio-8080-exec-3] o.a.c.c ...

  8. Spring Boot项目错误:Error parsing lifecycle processing instructions

    pom.xml文件错误:Error parsing lifecycle processing instructions 解决方法:清空.m2/repository下的所有依赖文件,重新下载即可解决该问 ...

  9. spring boot 运行错误: 找不到或无法加载主类

    在项目根目录运行 mvn clean install 进行重新编译 不行的话就删了原有的启动配置,重新配置启动.

  10. spring boot ----> 常用模板freemarker和thymeleaf

    ===========================freemarker=================================== freemarker 官网:https://freem ...

随机推荐

  1. ubuntu 16.04 docker下安装klee环境

    手动构建docker映象: 从github上获取klee源码 git clone https://github.com/klee/klee.git cd klee 使用存储库根目录中dockerfil ...

  2. Maven使用常用命令

    > mvn clean 删除target文件夹 > mvn clean test 编译测试代码,默认被放到target/test-classes文件夹下面 > mvn clean c ...

  3. 使用SharedPreference保存用户数据的步骤

    1. 声明 SharedPreferences sp; 2. 初始化 sp = this.getSharedPreferences("文件名", 0);//0代表的是私有 3. 获 ...

  4. 在 Vuejs 项目中如何定义全局变量 全局函数

    在 Vuejs 项目中如何定义全局变量 全局函数 在项目中,经常有些函数和变量是需要复用,比如说网站服务器地址,从后台拿到的:用户的登录 token, 用户的地址信息等,这时候就需要设置一波全局变量和 ...

  5. jquery获取input的checked属性

    1.经常需要判断某个按钮是否被选中. 2.基于jquery. <!DOCTYPE html> <html lang="en"> <head> & ...

  6. Python——可变类型与不可变类型(即为什么函数默认参数要用元组而非列表)

    Python 的内建标准类型有一种分类标准是分为可变类型与不可变类型: 可变类型:列表.字典 不可变类型:数字.字符串.元组 因为变量保存的实际都是对象的引用,所以在给一个不可变类型(比如 int)的 ...

  7. vue 分享知识点

    vue 分享模块清单 1.Vue 2.0之Vue实例和生命周期 2.vue 2.0之自定义指令 3.vue 2.0之观察者模式实现简单异步无限滚动 4.从JavaScript属性描述器剖析Vue.js ...

  8. Spanner:谷歌新一代全球部署的列式数据库

    Spanner 是一个可扩展的.全球分布式的数据库,提供分布式ACID. 架构 universe:一个部署的实例成为universe,目前谷歌有3个,分别为开发/测试/线上 Zone:一个数据中心,相 ...

  9. koa2开发入门

    一.koa2入门 1.创建koa2工程 首先,我们创建一个目录hello-koa并作为工程目录用VS Code打开.然后,我们创建app.js,输入以下代码: // 导入koa,和koa 1.x不同, ...

  10. Golang panic用法

    Go语言追求简洁优雅,所以,Go语言不支持传统的 try…catch…finally 这种异常,因为Go语言的设计者们认为,将异常与控制结构混在一起会很容易使得代码变得混乱.因为开发者很容易滥用异常, ...