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 ...
随机推荐
- nodeJS搭建一个简单的(代理)web服务器
前端获取数据时经常遇见跨域问题,以前一直用nginx做反向代理.最近在用vuejs,发现webpack-dev-server的代理简单好用.于是仿照写了一个简单的web服务器,用于非webpack的项 ...
- linux 手动释放buff/cache
为了解决buff/cache占用过多的问题执行以下命令即可 syncecho 1 > /proc/sys/vm/drop_cachesecho 2 > /proc/sys/vm/drop_ ...
- Spring Security构建Rest服务-0702-个性化用户认证流程2
登录成功后的处理AuthenticationSuccessHandler: 认证成功后,默认情况下spring security会继续访问之前访问的url,如果想自定义处理逻辑,用默认的就不行了.此时 ...
- Sequel-Model
Sequel::Model Mass Assignment 大多数的Model方法接受一个包含一系列key和value的哈希作为参数,这些方法包括:Model.new, Model.create, M ...
- Maven-pom.xml文件报错 Plugin execution not covered by lifecycle configuration
问题: Eclipse中新导入的项目pom.xml文件报错: Plugin execution not covered by lifecycle configuration: org.jacoco:j ...
- Zookeeper+ActiveMQ集群搭建
搭建三台虚拟机安装centos7.要提前安装好jdk环境 1.环境准备,搭建三台虚拟机ip分别是 192.168.192.130 192.168.192.131 192.168.192.134 Zoo ...
- inline-block各浏览器兼容以及水平间隙问题解决方案
inline-block属性 This value causes an element to generate a block box, which itself is flowed as a sin ...
- html学习笔记(一)
认识网页 网页组成 由文字.图片.输入框.视频.音频.超链接等组成. web标准 W3C组织(万维网联盟) Html (结构标准 ),相当人的身体. Css 样式(表现)标准 , 相当与给人化妆 变的 ...
- 机器学习--集成学习(Ensemble Learning)
一.集成学习法 在机器学习的有监督学习算法中,我们的目标是学习出一个稳定的且在各个方面表现都较好的模型,但实际情况往往不这么理想,有时我们只能得到多个有偏好的模型(弱监督模型,在某些方面表现的比较好) ...
- 05-python中的异常
python的所有的异常都继承自基类: Exception 处理方式和java类似: path = raw_input('input the path') array = path.split('/' ...