Spring Boot--01错误处理
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错误处理的更多相关文章
- Spring Boot自定义错误页面,Whitelabel Error Page处理方式
我已经是Spring Framework框架的忠实粉丝.对于企业软件开发者来说它提供了对常见问题的通用解决方案,包括那些你在未来开发中没有意识到的问题.但是,它构建的J2EE项目变得比较臃肿,需要被一 ...
- (后端)Spring Boot自定义错误页面,Whitelabel Error Page处理方式(转)
我已经是Spring Framework框架的忠实粉丝.对于企业软件开发者来说它提供了对常见问题的通用解决方案,包括那些你在未来开发中没有意识到的问题.但是,它构建的J2EE项目变得比较臃肿,需要被一 ...
- spring boot 启动错误:Could not resolve placeholder
在启动整个spring boot项目时,出现错误: Could not resolve placeholder 原因:没有指定好配置文件,因为src/main/resources下有多个配置文件,例如 ...
- Spring Boot自定义错误视图
Spring Boot缺省错误视图解析器 Web应用在处理请求的过程中发生错误是非常常见的情况,SpringBoot中为我们实现了一个错误视图解析器(DefaultErrorViewResolver) ...
- 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 ...
- Spring Boot → 01:概要
背景 大约20年前,程序员们使用“企业级Java Bean”(EJB)开发企业应用,需要配置复杂的XML. 在二十世纪初期,新兴Java技术——Spring,横空出世.使用极简XML和POJO(普通J ...
- 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 ...
- Spring Boot项目错误:Error parsing lifecycle processing instructions
pom.xml文件错误:Error parsing lifecycle processing instructions 解决方法:清空.m2/repository下的所有依赖文件,重新下载即可解决该问 ...
- spring boot 运行错误: 找不到或无法加载主类
在项目根目录运行 mvn clean install 进行重新编译 不行的话就删了原有的启动配置,重新配置启动.
- spring boot ----> 常用模板freemarker和thymeleaf
===========================freemarker=================================== freemarker 官网:https://freem ...
随机推荐
- zookeeper知识点学习
单机模式配置: Zookeeper 的启动脚本在 bin 目录下,Linux 下的启动脚本是 zkServer.sh 在你执行启动脚本之前,还有几个基本的配置项需要配置一 下,Zookeeper 的配 ...
- 开源视频会议bigbluebutton
这是另一个开源视频会议项目,简称bbb 官方网站:http://bigbluebutton.org/ 代码地址:https://code.google.com/p/bigbluebutton/ dem ...
- Android 开发工具类 17_setAlarm
Alarm 是在应用程序生命周期之外设置的,所以它们十分适合于调度定时更新或者数据查询,从而避免了在后台持续运行 Service.但触发 Alarm 时,就会广播指定的 Pending Intent. ...
- windows 下的bash 环境安装npm
1.Git下载 node.js下载2.安装 git 和 node.js3.将git\bin node.js安装目录加入环境变量path中4.在D盘下建立目录gitrep 打开Git Bash初始 ...
- js便签笔记(2)——DOM元素的特性(Attribute)和属性(Property)
1.介绍: 上篇js便签笔记http://www.cnblogs.com/wangfupeng1988/p/3626300.html最后提到了dom元素的Attribute和Property,本文简单 ...
- php的数组变量
数组就是存储同一类型的多个变量的 一种特殊的类型 php的数组有两种形态 1.普通类型 eg:$cars = array("Volvo","BMW"," ...
- C语言中判断字符串str1是否以str2开始或结束
#include <stdlib.h> #include <string.h> #include <stdio.h> /**判断str1是否以str2开头 * 如果 ...
- Java 裁剪图片
package com.test; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.Ima ...
- js 3行代码,最简易实现div效果悬浮
简易实现浮动效果的首要因素是:获取滚动条距离浏览器顶部的距离,下面直接贴代码: <!DOCTYPE html> <html> <head> <meta cha ...
- 不用bootstrap,只用CSS创建网格布局
本文译自[http://j4n.co/blog/Creating-your-own-css-grid-system],英语好的,可直接查看原网页,不需要FQ. 翻译拿不准的地方会有英文原文,方便大家理 ...