簡體   English   中英

@ExceptionHandler無法捕獲Spring Formatters拋出的異常

[英]@ExceptionHandler not catching exceptions being thrown from Spring Formatters

我有一個充當REST Web服務的控制器,我的方法之一是期望一個模型,在該模型中,我具有類型為MyObjectId的成員變量。

public class MyModel {

    private MyObjectId objectId;

    public MyObjectId getMyObjectId() {
        return objectId;
    }

    public void setMyObjectId(final MyObjectId objectId) {
        this.objectId = objectId;
    }
}

我為MyObjectId有一個Formatter (org.springframework.format)的自定義實現,這是我的解析方法:

@Override
public MyObjectId parse(final String text, final Locale locale) throws ParseException {
    // If the string is invalid it will throw an IllegalArgumentException
    return MyObjectIdConvertor.convert(text);
}

在我的spring-servlet.xml中,我注冊了格式化程序:

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="formatters">
        <set>
            <bean class="com.foo.MyObjectIdFormatter"/>
        </set>
    </property>    
</bean>

現在我的問題是,當從Formatter引發異常時,任何@ExceptionHandler方法都不會捕獲該@ExceptionHandler 因此,響應以以下格式輸出:

org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'filterModel' on field 'myObjectId': rejected value [foo123123]; codes [typeMismatch.filterModel.myObjectId,typeMismatch.myObjectId,typeMismatch.com.foo.MyObjectId,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [filterModel.myObjectId,myObjectId]; arguments []; default message [myObjectId]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.MyObjectId' for property 'myObjectId'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type com.MyObjectId for value 'foo123123'; nested exception is java.lang.NumberFormatException: For input string: "foo123123"]

如何使這種類型的異常開始被@ExceptionHandler 這是我的異常處理程序方法之一:

@ExceptionHandler(Exception.class)
@ResponseBody
public ResponseEntity<String> handleException(final Exception ex) {

    LOG.error("Error handling request", ex);
    return new ResponseEntity<>(StringUtils.hasText(ex.getMessage()) ? ex.getMessage() : "Error handling request", HttpStatus.BAD_REQUEST);
}

我有同樣的情況

我不處理@ControllerFormatter異常

檢查以下內容:

codes [typeMismatch.filterModel.myObjectId,typeMismatch.myObjectId,typeMismatch.com.foo.MyObjectId,typeMismatch];

我建議你解決ValidationMessages.properties (需要配置LocalValidatorFactoryBeanReloadableResourceBundleMessageSource

例如我有

typeMismatch.person.dateBirth=Invalid data, format for Date of Birth is incorrect!

然后假設明智和友好的用戶是否根據格式化程序寫入了無效的日期,就像您的情況一樣,我的DateFormatter類會引發異常,但是由於我定義了代碼typeMismatch.person.dateBirth因此錯誤消息會在我的Web表單中顯示。

觀察我們的代碼在某種程度上是相似的。 認為 Spring會在某些codes存在的地方顯示適當的消息,因為您沒有代碼,但異常堆棧跟蹤仍然保留(在我的情況下,代碼和自定義消息如何顯示在Web表單中)。

我還沒有測試該定制錯誤消息如何通過REST很好地適合。 但是就我而言,如果某些Formatter引發錯誤,我可以確定該code存在於ValidationMessages.properties文件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM