欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用 SpringMVC 优雅的处理异常 博客分类: springmvc springmvc 

程序员文章站 2024-03-16 19:54:28
...

1. 背景

小黄:

不同的 Controller 发生不同的异常, 我想要做不同的动作, 能不能简单配置优雅的实现?

比如 com.feilong.psi.controller.channel.ChannelController 如果出现com.feilong.psi.exception.SignNotEqualsException 那么跳转到404 页面 如果出现com.feilong.psi.exception.TradeStatusCanNotPayException 那么跳转到 首页

2. 实现

good question, 一看小黄就是个有追求的孩子, let's go~~

我们先来看如何实现,再来看原理

第1步 , 项目依赖 feilong spring

项目 pom.xml 添加 feilong spring 依赖

注: feilong spring 4.0.3 ,need spring 4 version

<project>
	....
	<properties>
		<version.feilong-spring>4.0.3</version.feilong-spring>
		....
	</properties>

	....
	<repositories>
		<repository>
			<id>feilong-repository</id>
			<url>https://raw.github.com/venusdrogon/feilong-platform/repository</url>
		</repository>
	</repositories>

	....
	<dependencies>
		....
		<dependency>
			<groupId>com.feilong.platform.spring</groupId>
			<artifactId>feilong-spring-all</artifactId>
			<version>${version.feilong-spring}</version>
		</dependency>
		....
	</dependencies>
	....

第2步 使用 com.feilong.spring.web.servlet.handler.SimpleMappingHandlerMethodExceptionResolver

新建个 springmvc-pay-channel-exception.xml 文件

...
<!-- 通道异常解决器 -->
<bean id="channelExceptionResolver" class="com.feilong.spring.web.servlet.handler.SimpleMappingHandlerMethodExceptionResolver">
    <property name="order" value="1" />
    <property name="preventResponseCaching" value="true" />
    <property name="mappedHandlerClasses">
        <array>
            <value>com.feilong.psi.controller.channel.ChannelController</value>
        </array>
    </property>

    <property name="exceptionMappings">
        <props>
            <!-- 安全签名参数不正确的异常. -->
            <prop key="com.feilong.psi.exception.SignNotEqualsException">redirect:/errors/404</prop>

            <!-- 交易的状态不能去支付的异常.
             since 20180920 change from 500 to /
             -->
            <prop key="com.feilong.psi.exception.TradeStatusCanNotPayException">redirect:/</prop>
        </props>
    </property>
</bean>
...

第3步 加载上面配置的 channelExceptionResolver

springmvc-exceptionResolver.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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <import resource="classpath:springmvc/pay/springmvc-pay-channel-exception.xml" />
	<import resource="classpath:springmvc/pay/springmvc-pay-redirect-exception.xml" />
	<import resource="classpath:springmvc/pay/springmvc-pay-notify-exception.xml" />

    <!-- exceptionResolver -->
    <bean id="exceptionResolver" class="com.baozun.exception.ExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="com.baozun.exception.BusinessException">forward:/errors/error</prop>
                <prop key="java.lang.Throwable">forward:/errors/error</prop>
            </props>
        </property>
    </bean>

</beans>

注意 要保证公司全局的 com.baozun.exception.ExceptionResolver 在最下面

3. 原理

feilong com.feilong.spring.web.servlet.handler.SimpleMappingHandlerMethodExceptionResolver 借鉴于 SimpleMappingExceptionResolver , 并在此基础上进行了扩展 ,参见扩展5

4. 扩展- 使用自定义 view

小李:

我看到在上面的 xml 中可以直接配置 redirect:/errors/404 就跳转到404 页面, 我现在有个支付通知controller , 如果业务正常处理, 响应支付网关 Continnue 字符串, 如果有异常 我要返回 stop 字符串, 这个 stop 我想在 xml 中配置 该如何实现呢?

good question

你可以使用feilong spring 自定义的 printwrite view

springmvc-viewResolver-printwrite.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util" 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.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

    <bean class="com.feilong.spring.web.servlet.view.printwriter.PrintWriterViewResolver">
        <property name="order" value="-1" />
        <property name="prefix" value="printwrite:" />
    </bean>
</beans>

springmvc-pay-notify-exception.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 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.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

    <!-- doku notify异常解决器 -->
    <bean id="dokuNotifyExceptionResolver" class="com.feilong.spring.web.servlet.handler.SimpleMappingHandlerMethodExceptionResolver">
        <property name="order" value="1" />
        <property name="preventResponseCaching" value="true" />
        <property name="mappedHandlerClasses">
            <array>
                <value>com.feilong.psi.doku.controller.notify.DokuNotifyController</value>
            </array>
        </property>

        <property name="exceptionMappings">
            <props>
                <!-- STOP -->
                <prop key="com.feilong.psi.exception.PsiNotifyException">printwrite:STOP</prop>
                <prop key="java.lang.Throwable">printwrite:STOP</prop>
            </props>
        </property>
    </bean>
</beans>

5. 扩展- 使用自定义 exceptionAndExceptionViewNameBuilderMap

在实际的业务场景中, 简单的 properties 结构的 exceptionMappings 可能不能满足业务需求

比如,跳转的url 地址需要使用部分参数

此时你使用 exceptionAndExceptionViewNameBuilderMap 属性, 配置成如下格式

<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 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.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

    <!-- Doku Redirect 异常解决器 -->
    <bean id="redirectExceptionResolver" class="com.feilong.spring.web.servlet.handler.SimpleMappingHandlerMethodExceptionResolver">
        <property name="order" value="1" />
        <property name="preventResponseCaching" value="true" />
        <property name="mappedHandlerClasses">
            <array>
                <value>com.feilong.psi.doku.controller.redirect.DokuRedirectController</value>
            </array>
        </property>

        <property name="exceptionAndExceptionViewNameBuilderMap">
            <map>
                <entry key="com.feilong.psi.exception.PayResultStatusNotPaidException" value-ref="payResultStatusNotPaidExceptionViewNameBuilder"/>
            </map>
        </property>
    </bean>

	<bean id="payResultStatusNotPaidExceptionViewNameBuilder" class="com.baozun.store.web.controller.payment.builder.PayResultStatusNotPaidExceptionViewNameBuilder"/>

</beans>

关于 PayResultStatusNotPaidExceptionViewNameBuilder

public class PayResultStatusNotPaidExceptionViewNameBuilder extends AbstractExceptionViewNameBuilder{

    @Override
    public String build(HandlerMethod handlerMethod,Exception exception,HttpServletRequest request,HttpServletResponse response){
        PayResultStatusNotPaidException notPaidException = (PayResultStatusNotPaidException) exception;
        return buildViewName(request,notPaidException);
    }

    private String buildViewName(HttpServletRequest request,PayResultStatusNotPaidException notPaidException){
        String tradeNo = notPaidException.getTradeNo();
        String s = PaymentSecureBuilder.buildS(getMemberId(request).toString(), S_NAME_RESULT, tradeNo);
        return "redirect:" + SECURE_PAYMENT_FAIL_URL + "?transactionId=" + tradeNo + "&s=" + s;
    }
    ...
}

注意 : 优先级 exceptionAndExceptionViewNameBuilderMap > exceptionMappings 属性

 

相关标签: springmvc