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 ...
随机推荐
- Tiny4412 Linux 内核配置流程
1.配置交叉编译器 默认情况下,内核构建的是与宿主机相同的体系架构镜像.如果要交叉编译,需要设置两个变量ARCH和CORSS_COMPILE. ①ARCH:指明目标体系架构,如x86.arm.mips ...
- Hive UDF开发 第一个例子
package udf; import org.apache.hadoop.hive.ql.exec.UDF; public class helloudf extends UDF{ public St ...
- python爬虫实战(3)--图片下载器
本篇目标 1.输入关键字能够根据关键字爬取百度图片 2.能够将图片保存到本地文件夹 1.URL的格式 进入百度图片搜索apple,这时显示的是瀑布流版本,我们选择传统翻页版本进行爬取.可以看到网址为: ...
- struts-hibernate整合(1)配置环境
①加载jar包 创建类库: 在myeclipse中点击windows---Preference---Java---Build Path---User Libraries---new 输入创建类库名字s ...
- RxAndroid基本使用1
1,基本使用 public class MainActivity extends ActionBarActivity implements View.OnClickListener, View.OnT ...
- cocos2d-js 骨骼动画 3.10
近期使用了cocos动画中的骨骼动画,这里记录使用的两种方式(3.10版): 一.cocos自带的动画编辑器导出的动画 ccs.armatureDataManager.addArmatureFileI ...
- tee 可以看见输出并将其写入到一个文件中
如下使用tee命令在屏幕上看见输出并同样写入到日志文件my.log中: [root@localhost home]# top |tee my.log tee可以保证你同时在屏幕上看到top的 ...
- 算法Sedgewick第四版-第1章基础-022一QueueWithTwoStacks
/****************************************************************************** * Compilation: javac ...
- [转载]/etc/security/limits.conf解释及应用
limits.conf的格式如下: username|@groupname type resource limit username|@groupname:设置需要被限制的用户名,组名前面加@和用户名 ...
- bzoj2134 错选单位
传送门 题目 Input n很大,为了避免读入耗时太多, 输入文件只有5个整数参数n, A, B, C, a1, 由上交的程序产生数列a. 下面给出pascal/C/C++的读入语句和产生序列的语句( ...