0

我正在尝试在 spring mvc 3.2 中使用 ExceptionHandler 注释的方法中使用自定义参数来处理异常。但是,当执行该方法时,我仍然会遇到此异常: java.lang.IllegalStateException: No proper resolver for argument[1] [type=com.example.domain.CustomArgument]

控制器方法如下所示:

@ExceptionHandler(IOException.class)
@ResponseBody
public Error handleIOException(IOException ex, CustomArgument customArgument) {
    return new Error(customArgument.getMessage());
}

我正在使用以下 xml 配置:

<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean class="com.example.spring.CustomArgumentWebArgumentResolver" scope="singleton">
            <constructor-arg ref="customArgumentService" />
        </bean>
    </mvc:argument-resolvers>
</mvc:annotation-driven>

<bean id="customArgumentService" class="com.example.service.CustomArgumentService" scope="singleton" />

<bean class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver" scope="singleton">
    <property name="customArgumentResolvers">
        <list>
            <bean class="com.example.service.CustomArgumentService" scope="singleton">
                <constructor-arg ref="customArgumentService" />
            </bean>
        </list>
    </property>
</bean>

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" scope="singleton">
    <property name="defaultErrorView" value="forward:/error" />
</bean>

而且我相信 mvc:annotation-driven 已经分配了一个 ExceptionHandlerExceptionResolver,那么我该如何添加 customArgumentResolver。任何帮助将不胜感激。

4

1 回答 1

0

对于几个 ExceptionResolver,请尝试使用 HandlerExceptionResolverComposite。像这样的东西:

<bean class="org.springframework.web.servlet.handler.HandlerExceptionResolverComposite">
    <property name="exceptionResolvers">
        <list>
            <bean class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver">
                <constructor-arg ref="exceptionHandlerExceptionResolver" />
            </bean>
            <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
                <constructor-arg ref="simpleMappingExceptionResolver" />
            </bean>
        </list>
    </property>
    <property name="order" value="0"/>
</bean>
于 2013-07-10T05:38:27.577 回答