springmvc全局异常@ControllerAdvice拦截无效问题解决

项目场景:

项目服务端采用springmvc架构


问题描述:

发现请求的业务逻辑抛出异常后,全局异常无法处理。 自己写的全局异常处理类如下代码:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiliao.util.MessageUtil;

/**
 * 全局Controller层异常处理类
 */
@ControllerAdvice
public class GlobalExceptionResolver {

	private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionResolver.class);

	@ExceptionHandler(DuplicateKeyException.class)
	@ResponseBody
	public MessageUtil handleDuplicateKeyException(DuplicateKeyException e) {
		logger.error(e.getMessage(), e);
		return new MessageUtil(0, "该数据已存在");
	}

	@ExceptionHandler(BadSqlGrammarException.class)
	@ResponseBody
	public MessageUtil handleBadSqlGrammarException(BadSqlGrammarException e) {
		logger.error(e.getMessage(), e);
		return new MessageUtil(0, "系统未知错误(BadSql)");
	}

	@ExceptionHandler(Exception.class)
	@ResponseBody
	public MessageUtil handleException(Exception e) {
		logger.error(e.getMessage(), e);
		return new MessageUtil(0, "操作异常");
	}

	@ExceptionHandler(Throwable.class)
	@ResponseBody
	public MessageUtil handleThrowable(Throwable e) {
		logger.error(e.getMessage(), e);
		return new MessageUtil(0, "操作异常");
	}

	@ExceptionHandler(BusinessException.class)
	@ResponseBody
	public MessageUtil handleBusinessException(BusinessException e) {
		logger.error(e.getMessage(), e);
		return new MessageUtil(0, e.getMessage());
	}

}

springmvc.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:task="http://www.springframework.org/schema/task" xmlns:cache="http://www.springframework.org/schema/cache"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"
	default-lazy-init="true">

	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" />

	<!-- aop配置 -->
	<aop:aspectj-autoproxy />


	<!-- 默认的注解映射的支持 -->
	<context:annotation-config />

	<!-- 自动扫描的包名 -->
	<context:component-scan base-package="com.yiliao">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
	</context:component-scan>

	<!-- 视图解释类 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/share/" />
		<property name="suffix" value=".jsp" /><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
	</bean>

	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>text/html;charset=UTF-8</value>
						</list>
					</property>
				</bean>
			</list>
		</property>
	</bean>
</beans>

原因分析:

Springmvc没有注入正确的异常解析处理类,如下注入的是org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver.class
在这里插入图片描述
而正确的应该是org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.class


解决方案:

在springmvc.xml配置文件中添加如下代码:

<mvc:annotation-driven/>

该注解会正确注入异常解析类如下图:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值