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 ...
随机推荐
- java判断姓是否合格 千家姓
package com.sycx.domain; import java.lang.reflect.Array; public class FirstName { public static bool ...
- GET与POST方法
HTTP中的GET,POST,PUT,DELETE对应着对这个资源的查,改,增,删4个操作.GET一般用于获取/查询资源信息,而POST一般用于更新资源信息. 1.根据HTTP规范,GET用于信息获取 ...
- 数据库开源框架ormlite
今天听说了ORM框架ORMLITE,特地去了解了一下. 该框架可以使用注解方式来生成数据库表,还封装了常用的数据库操作. 类似J2EE的HIBERNATE框架对数据库的处理. 省去了书写建表语句的麻烦 ...
- 使用ServerSocket建立聊天服务器(一)
-------------siwuxie095 工程名:TestMyServerSocket 包名:com.siwuxie095.socket ...
- bluebird的安装配置
安装 下载bluebird 3.5.0(开发) 意味着在开发中使用的未分类源文件.警告和长堆栈跟踪被启用,这会影响性能. <script src="//cdn.jsdelivr.net ...
- UIScrollView现实循环滚动
#import "RootViewController.h" #define width [UIScreen mainScreen].bounds.size.width #defi ...
- opencv新版本的数据结构
转自 http://blog.csdn.net/yang_xian521/article/details/7108387 记得我在OpenCV学习笔记(四)——新版本的数据结构core里面讲过新版本的 ...
- [gist]Android SHA-1
参考:http://stackoverflow.com/questions/5980658/how-to-sha1-hash-a-string-in-android 代码:
- Luogu 4363 [九省联考2018]一双木棋chess
发现数据范围很小,想到状压dp,然后就愣住不会了. 表示太菜了并没有接触过轮廓线dp这种操作. 首先发现合法的操作过程中一定是这样子的: 按照行来看发现每一行单调不递增. 我们用$1$来表示竖着的轮廓 ...
- 数据库commit问题
对数据库进行修改后,需要commit!---之前也是忘记commit导致数据库反应不过来.