博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
@ControllerAdvice注解的使用
阅读量:4699 次
发布时间:2019-06-09

本文共 1583 字,大约阅读时间需要 5 分钟。

package com.vcredit.ddcash.monitor.controller;

import com.vcredit.ddcash.monitor.model.dto.Response;

import freemarker.template.utility.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**

* Created by guanshuli on 2017/1/11.
*/
@ControllerAdvice
public class GlobalExceptionHandler {
public static final String DEFAULT_ERROR_VIEW = "error";
public static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

@ExceptionHandler(value = Throwable.class)

@ResponseBody
public Object defaultErrorHandler(Throwable e,
HttpServletRequest request) {
Response response=new Response();
e.printStackTrace();
response.setStatus("接口异常");
response.setMsg("网络异常,请稍后重试");
this.writeLog(request, e);
return response;
}

private void writeLog(HttpServletRequest request, Throwable e) {

Map<String, String> map = new HashMap<String, String>();
Enumeration<String> enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
String str = enumeration.nextElement().toString();
map.put(str, request.getHeader(str));
}
map.put("getRequestURI", request.getRequestURI());
logger.error("请求异常,请求头:{};", map.toString(), e);

}

}

转载于:https://www.cnblogs.com/muliu/p/6273740.html

你可能感兴趣的文章
python开发学习-day03(set集合、collection系列 、深浅拷贝、函数)
查看>>
yii性能调节
查看>>
centos 7.4 + postgresql 10.1 + pg_amqp
查看>>
Throwable、Error、Exception、RuntimeException 区别
查看>>
js对闭包的理解
查看>>
腾讯面试问题
查看>>
自定义标题栏右键菜单
查看>>
Bootstrap概述
查看>>
elementUi源码解析(1)--项目结构篇
查看>>
C#中用DateTime的ParseExact方法解析日期时间(excel中使用系统默认的日期格式)
查看>>
任务二 阅读报告
查看>>
高阶函数
查看>>
W3100SM-S 短信猫代码发送 上
查看>>
打开一个页面,并监听该页面的关闭事件
查看>>
软件保护技术--- 常见保护技巧
查看>>
SQL批量分离工具
查看>>
Erlang与ActionScript3采用JSON格式进行Socket通讯
查看>>
python数据类型--数字、字符串
查看>>
CentOS7使用firewalld打开关闭防火墙与端口
查看>>
Servlet实现图片读取显示
查看>>