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. 面试题:js如何渲染十万条数据并不卡住界面

    这道题考察了如何在不卡住页面的情况下渲染数据,也就是说不能一次性将几万条都渲染出来,而应该一次渲染部分 DOM,那么就可以通过 requestAnimationFrame 来每 16 ms 刷新一次. ...

  2. java验证码工具

    工具类: package com.lhy.web.servlet; import java.awt.BasicStroke; import java.awt.Color; import java.aw ...

  3. heroku 部署ruby项目后 未连接数据库显示(We're sorry, but something went wrong. If you are the application owner )

    如何部署请参照: http://blog.csdn.net/xz360717118/article/details/62422741 部署后如果发现显示:We're sorry, but someth ...

  4. 如何虚拟机里安装Win10操作系统

    不多说,直接上干货! Windows Server 2003.2008.2012系统的安装 推荐网址:打开MSDN网站(http://msdn.itellyou.cn ) 关于给电脑换系统,很多人会花 ...

  5. OpenGL11-绘制汉字最高效方法(使用Freetype)(代码已更新)

    最新版本,之前的版本有些文件没有打包 视频教程请关注 http://edu.csdn.net/lecturer/lecturer_detail?lecturer_id=440 OpenGL本身并没有绘 ...

  6. arm处理器启动流程分析

    2440: 启动方式:nor , nand 地址布局: 启动流程: 开发板在上电后,会从0x0地址处运行. 如果从nor flash启动,则代码要放在nor 的0地址处: 如果从nand flash启 ...

  7. leetcode--539. Minimum Time Difference

    Given a list of -hour clock time points in "Hour:Minutes" format, find the minimum minutes ...

  8. ls命令显示的total你知道代表着什么吗?

    今天我无意间在用ls命令的时候发现显示的内容里的total,这个total代表着什么,引起了我的疑惑. 接下来开始解开它的神秘面纱. total后面的数字是指当前目录下所有文件所占用的空间总和,它是怎 ...

  9. ABP实战--集成Ladp/AD认证

    参照Hunter的ABP-Zero模块中用户管理部分. 由于我们公司的各系统基本都是AD帐号登录的,所以我们需扩展ABP的AuthenticationSource. 添加MyLdapAuthentic ...

  10. [PY3]——logging

    logging模块的logger.handler.filter.formatter Logger记录器 提供日志接口,供应用代码使用.logger最长用的操作有两类:配置和发送日志消息.可以通过log ...