【问题标题】:@ExceptionHandler not being invoked@ExceptionHandler 未被调用
【发布时间】:2012-06-02 22:52:52
【问题描述】:

经过一段时间的研究和尝试不同的事情后,我仍然无法在我的 jUnit 集成测试中调用我的 @ExceptionHandler。请帮我理解为什么?

@RequestMapping(value = "/someURL", method = RequestMethod.POST)
public ModelAndView batchUpload(@RequestBody final String xml, @RequestParam boolean replaceAll)
    throws IOException, URISyntaxException, SAXException, ParserConfigurationException, InvocationTargetException, IllegalAccessException, UnmarshallingFailureException
{
StreamSource source = new StreamSource(new StringReader(xml));
DomainClass xmlDomainClass;

try
{
    xmlDomainClass = (DomainClass) castorMarshaller.unmarshal(source);
}
catch (UnmarshallingFailureException me)
{
    // some logging. this gets executed
    throw me;
}

.

@ExceptionHandler(Throwable.class)
public ModelAndView handleUnmarshallingExceptions(Throwable th)
{
// never executes anything in here
return new ModelAndView( /*some parameters */ );
}

【问题讨论】:

    标签: spring exception-handling


    【解决方案1】:

    Spring: RESTful controllers and error handling 帮助了我,我的问题是缺少这个:

    @Component
    public class AnnotatedExceptionResolver extends AnnotationMethodHandlerExceptionResolver{
      public AnnotatedExceptionResolver() {
        setOrder(HIGHEST_PRECEDENCE);
      }
    }
    

    【讨论】:

    • 很好的答案 - 我无法弄清楚为什么我的 @ExceptionHandler 被忽略,但我认为这可能与我也在使用 SimpleMappingExceptionResolver 的事实有关。这为我解决了这个问题。
    猜你喜欢
    • 2016-08-01
    • 2014-04-05
    • 2013-04-16
    • 2018-05-26
    • 1970-01-01
    • 2019-10-13
    • 2021-05-27
    • 2018-12-03
    • 2021-12-08
    相关资源
    最近更新 更多