首先,在我们的工程中新建BaseController父类,内容如下:

package com.ztesoft.zsmartcity.framework.exception;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
* 2015-11-06
*
* @author djw 统一异常处理,所有controler继承此类
*/
public abstract class BaseController {
@ExceptionHandler
public String handleException(HttpServletRequest request,
HttpServletResponse response, Exception e) {

printException(e);// log4j打印错误级别堆栈日志信息,时间
// json格式的ajax请求
if (request.getHeader("accept").indexOf("application/json") > -1
|| (request.getHeader("X-Requested-With") != null && request
.getHeader("X-Requested-With")
.indexOf("XMLHttpRequest") > -1)) {
response.setStatus(500);
response.setContentType("application/json;charset=utf-8");
try {
PrintWriter writer = response.getWriter();
if (e instanceof RuntimeException) {// 运行时异常
writer.write("系统内部异常!");
} else {// 非运行时异常
writer.write(e.getMessage());// 此处待细化异常处理给提示 ???
}
writer.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
} else {// URL普通请求
if (e instanceof RuntimeException) {// 运行时异常
request.setAttribute("exceptionMessage", "系统内部异常!");
} else {
request.setAttribute("exceptionMessage", e.getMessage());// 此处待细化异常处理给显示 ???
}
try {// 跳转统一异常处理界面
request.getRequestDispatcher("../error.jsp").forward(
request, response);
} catch (ServletException | IOException e1) {
e1.printStackTrace();
}
return null;
}
}

public void printException(Exception e) {
StringWriter sw = null;
PrintWriter pw = null;
try {
sw = new StringWriter();
pw = new PrintWriter(sw);
// 将出错的栈信息输出到printWriter中
e.printStackTrace(pw);
pw.flush();
sw.flush();
} finally {
if (sw != null) {
try {
sw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (pw != null) {
pw.close();
}
}
Logger.getLogger(BaseController.class).error(
new Date() + ":" + sw.toString());
}

}

所有controler类继承此类实现异常统一处理;

spring @ExceptionHandler注解方式实现异常统一处理的更多相关文章

  1. spring boot / cloud (十二) 异常统一处理进阶

    spring boot / cloud (十二) 异常统一处理进阶 前言 在spring boot / cloud (二) 规范响应格式以及统一异常处理这篇博客中已经提到了使用@ExceptionHa ...

  2. (转)使用Spring的注解方式实现AOP的细节

    http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...

  3. mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类

    相信大家在刚开始学习mybatis注解方式,或者spring+mybatis注解方式的时候,一定会有一个疑问,为什么mybatis的dao接口只需要一个接口,不需要实现类,就可以正常使用,笔者最开始的 ...

  4. (转)使用Spring的注解方式实现AOP入门

    http://blog.csdn.net/yerenyuan_pku/article/details/52865330 首先在Eclipse中新建一个普通的Java Project,名称为spring ...

  5. spring注解方式,异常 'sessionFactory' or 'hibernateTemplate' is required的解决方法

    做单元测试的时候,抛出异常 Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' ...

  6. spring 纯注解方式 与AOP

    spring注解方式 以前我也使用过纯注解方式.现在在这里做个记录 我们先认识几个我们都耳熟能详的注解 @configuration :从spring3.0这个注解就可以用于定义配置类,可以替换xml ...

  7. spring IOC注解方式详解

    本文分为三个部分:概述.使用注解进行属性注入.使用注解进行Bean的自动定义. 一,概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以 ...

  8. Spring的注解方式

    在Java代码中可以使用@Resource或者@Autowired注解方式来经行注入.虽然@Resource和@Autowired都可以来完成注入依赖,但它们之间是有区别的. a.@Resource默 ...

  9. Spring 使用注解方式进行事务管理

    转载:http://www.cnblogs.com/younggun/archive/2013/07/16/3193800.html 使用步骤: 步骤一.在spring配置文件中引入<tx:&g ...

随机推荐

  1. C#之参数线程

    public Form1() { InitializeComponent(); } Thread t; private void button1_Click(object sender, EventA ...

  2. js遍历

    最近看了一些不错的文章关于js遍历+js数组去重+文件上传的,今天也自己动手试了试.有好多之前不是细节不是很了解.正好学习了. map函数也是 类似这样的对象还有函数的属性arguments对象,当然 ...

  3. AB串(上帝都不会,我就没救了)

    [题目分析] 设答案的长度为m,

  4. 通用的linux下安装配置svn独立服务

    参考资料: http://www.blogjava.net/zhouf/articles/251476.html http://www.cnblogs.com/thinksasa/archive/20 ...

  5. Linux 常见的trouble shooting故障排错

    Linux 常见的trouble shooting故障排错 备份开机所必须运行的程序对一个运维人员来说是非常有必要的.在实际生产环境中,系统和数据基本都是安装在不同的硬盘上面,因为企业最关心的还是数据 ...

  6. fragment (1)简单示例:定义,界面配置,fragment之间的跳转

    fragment作用 同一程序中切换界面 比activity轻快,灵活. fragment代码示例 ide  : android studio 1.2 sdk : 22 package com.exa ...

  7. Java中HashMap遍历的两种方式

    Java中HashMap遍历的两种方式 转]Java中HashMap遍历的两种方式原文地址: http://www.javaweb.cc/language/java/032291.shtml 第一种: ...

  8. JAVA常用数据结构及原理分析

    JAVA常用数据结构及原理分析 http://www.2cto.com/kf/201506/412305.html 前不久面试官让我说一下怎么理解java数据结构框架,之前也看过部分源码,balaba ...

  9. JavaScript的条件语句

    JavaScript的条件语句 1.JavaScript的条件语句包括以下几个 (1)if - 只有当指定条件为true时,使用该语句来执行代码: (2)if...else - 当指定条件为true时 ...

  10. iOS奔溃日志总结

    1,http://www.cnblogs.com/qingjoin/p/3515902.html 2,http://blog.csdn.net/u012269653/article/details/4 ...