spring 和 spirngMvc 中 异常处理
spring 中 的 异常处理 使用的是aspectj
@Aspect
@Component
/**
*
* @author ****
* @createData 2017年7月13日 上午8:36:23
* @说明 :出了一些空值。。。
*/
public class AjaxEntityHandler { // @Pointcut("@annotation(org.zkdg.utils.annotations.AfterHandler)") @Pointcut("@annotation(org.zkdg.utils.spring.annotations.NotNullVariable)")
// @Pointcut("execution(* org.dcexam.*.service.*.*(..))")
public void beforeCall() {
// service方法调用之前,检测参数,仅限第一个参数, 不能为空值
} /**
* service发生异常时调用
*/
@Pointcut("execution(* org.dcexam.*.service.*.*(..))")
public void afterThrowEx() {
System.out.println("************\n\n\n\n\n\n\n\n\n\n\n\n*******");
} @Around(value = "beforeCall()")
public AjaxEntity doBefore(ProceedingJoinPoint point) throws Throwable {
// TODO Auto-generated method stub
Object[] args = point.getArgs();
if (args == null || args[0] == null) {
return new AjaxEntity("warning", "未选择任何数据。。。");
}
if (args[0] instanceof String) {
String str = (String) args[0];
if (str.equalsIgnoreCase(""))
return new AjaxEntity("warning", "未选择任何数据。。。");
} AjaxEntity ajax = (AjaxEntity) point.proceed(args); return ajax == null ? AjaxEntity.ERROR("操作失败") : ajax;
} /**
*
* @param joinPoint
* 连接点
* @param ex
* 异常
* @return AjaxEntity 异常信息
*/
@AfterThrowing(value = "afterThrowEx()", throwing = "ex")
public void doAfterThrowEx(JoinPoint joinPoint, Exception ex) {
AjaxEntity ajax = new AjaxEntity(); if (ex.getCause() instanceof SQLException) {
// 数据库操作异常
ajax = AjaxEntity.ERROR("操作数据库时出现异常");
} } }
spring.xml 中 配置
<!-- 注解aop,支持注解 -->
<aop:aspectj-autoproxy />
事务 切点配置
<!-- 配置参与事务的类 -->
<aop:config expose-proxy="true" proxy-target-class="true">
<aop:pointcut id="txPointCut"
expression="execution(* org.dcexam.*.service.*.*(..))" />
<aop:advisor pointcut-ref="txPointCut" advice-ref="txAdvice" />
</aop:config>
注意 expose-proxy="true" proxy-target-class="true" 是 aspectj 代理
在springMvc 中 ,由于 spring 与 springmvc 为 不同的容器。尽量不要使用aspecj代理 ,使用spring mvc 自带的 HandlerExceptionResolver 处理 异常
package org.zkdg.utils.spring.interceptor; import java.sql.SQLException; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.zkdg.utils.entity.AjaxEntity;
import org.zkdg.utils.util.JsonUtils; /**
*
* @author
* @createData 2017年7月13日 下午12:27:19
* @说明 :springMvc 异常处理
*/
// 注解,被spring 扫描到
@Component
public class ExInterceptor implements HandlerExceptionResolver { @Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex) {
// TODO Auto-generated method stub
if (ex != null) {
try {
response.setStatus(200);
Throwable cause = ex.getCause();
if (cause == null) {
response.getWriter().write(JsonUtils.objectToJson(AjaxEntity.ERROR("<p style='color:red'>空指针异常</p> ")));
} else if (cause instanceof SQLException) {
// json 输出
response.getWriter()
.write(JsonUtils.objectToJson(AjaxEntity.ERROR("数据库操作失败 : <p style='color:red'>" + cause.getMessage()+"</p>")));
} else if (cause instanceof NullPointerException) {
response.getWriter()
.write(JsonUtils.objectToJson(AjaxEntity.ERROR("空指针异常 : <p style='color:red'>" + cause.getMessage()+"</p>")));
} else {
response.getWriter()
.write(JsonUtils.objectToJson(AjaxEntity.ERROR("未知异常 : <p style='color:red'>" + cause.getMessage()+"</p>")));
}
} catch (Exception e) {
// TODO Auto-generated catch block e.printStackTrace();
} }
ex.printStackTrace();
// 返回一个空的 modelandview(),必须返回,否则 异常处理配置无效。。
return new ModelAndView();
} }
不得不说,spring 是真的太强大了!!!!
spring 和 spirngMvc 中 异常处理的更多相关文章
- Spring Cloud Gateway中异常处理
最近我们的项目在考虑使用Gateway,考虑使用Spring Cloud Gateway,发现网关的异常处理和spring boot 单体应用异常处理还是有很大区别的.让我们来回顾一下异常. 关于异常 ...
- springcloud Zuul中异常处理细节
Spring Cloud Zuul对异常的处理整体来说还是比较方便的,流程也比较清晰,只是由于Spring Cloud发展较快,各个版本之间有差异,导致有的小伙伴在寻找这方面的资料的时候经常云里雾里, ...
- 转:Spring Boot应用中的异常处理
引自:https://www.cnblogs.com/yangfanexp/p/7616570.html 楼主前几天写了一篇“Java子线程中的异常处理(通用)”文章,介绍了在多线程环境下3种通用的异 ...
- 基于spring注解AOP的异常处理
一.前言 项目刚刚开发的时候,并没有做好充足的准备.开发到一定程度的时候才会想到还有一些问题没有解决.就比如今天我要说的一个问题:异常的处理.写程序的时候一般都会通过try...catch...fin ...
- SpringMVC中异常处理详解
Spring MVC处理异常最基本的就是HandlerExceptionResolver这个接口,先看张图 分析上图可以轻松总结出,spring mvc里有三种异常处理方法: 1.使用官方提供的简单异 ...
- Spring cloud Zuul网关异常处理
Spring cloud Zuul网关异常处理 一 异常测试: 1> 创建一个pre类型的过滤器,并在该过滤器的run方法实现中抛出一个异常.比如下面的实现,在run方法中调用的doSometh ...
- spring 或 springboot统一异常处理
spring 或 springboot统一异常处理https://blog.csdn.net/xzmeasy/article/details/76150370 一,本文介绍spring MVC的自定义 ...
- 基于Spring Boot的统一异常处理设计
基于Spring Boot的统一异常处理设计 作者: Grey 原文地址:https://www.cnblogs.com/greyzeng/p/11733327.html Spring Boot中,支 ...
- 在Spring tools suite中使用git 共享项目
我们都在eclipse 和 myeclipse中使用过cvs 和 svn 版本控制工具进行团队开发,今天我学习了另外一种版本控制工具git,下面我演示如何在Spring tools suite中使用g ...
随机推荐
- Linux系统下Oracle执行SQL脚本后中文出现乱码解决方法
先确认Oracle的字符集,sqlplus登录Oracle后执行语句: [sql] select userenv('language') from dual; 返回值例如:AMERICAN_AME ...
- Android Studio 搭配 Tortoise SVN 安装问题汇总
(1)Android studio 中想要使用SVN,但是在安装 1.9版本的SVN,会报SVN is too old(实际是太新了)的错误.所以只能下载1.8以下版本 (2)安装svn时,需要手动选 ...
- IP及端口号
IP:代表一台机器 端口号:每一个程序都有一个端口号与之对应 一个域名对应一个虚拟主机
- day35-hibernate映射 04-Hibernate的一级缓存:一级缓存的存在
数据源:文件和数据库.从内存中获取,不用去数据库发送SQL语句查询了.缓存技术是hibernate的一个优化的手段.Session结束了,一级缓存就没了,就销毁了.SeesionFactory没了,二 ...
- 猪羊——HTML解析
HTML标签和属性大全见:http://www.cnblogs.com/Mr-liyang/p/5797976.html CSS样式大全:http://www.cnblogs.com/Mr-liyan ...
- opencv新版本的数据结构
转自 http://blog.csdn.net/yang_xian521/article/details/7108387 记得我在OpenCV学习笔记(四)——新版本的数据结构core里面讲过新版本的 ...
- 6.Model类
Basic Concepts 在Model/View结构中,Model提供标准的接口让View和Delegate获得数据.在QT中,标准的接口都被定义在QAbstractItemModel类 ...
- DIY的RPM包怎么签名呢 - 笔记
参考 https://gist.github.com/fernandoaleman/1376720 如果打不开上一个连接,请参考https://www.cnblogs.com/LiuYanYGZ/p/ ...
- Linux之tcpdump使用详解
1.1 三种关键字 关于类型的关键字 第一种是关于类型的关键字,主要包括host,net,port, 例如 host 210.27.48.2,指明 210.27.48.2是一台主机,net 202. ...
- kafka的producer执行卡住的问题
使用windows开发producer然后向远程的kakfa集群发送数据,但是一直卡着, 在window的hosts文件添加kafka集群的主机名和ip的映射就好了 网上搜了下,大致是producer ...