项目场景:

项目服务端采用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/>

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

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐