简体   繁体   English

@ExceptionHandler无法捕获Spring Formatters抛出的异常

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

I have a controller that serves as REST web service and one of my methods is expecting a model which in this model I have a member variable of type MyObjectId . 我有一个充当REST Web服务的控制器,我的方法之一是期望一个模型,在该模型中,我具有类型为MyObjectId的成员变量。

public class MyModel {

    private MyObjectId objectId;

    public MyObjectId getMyObjectId() {
        return objectId;
    }

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

I have a custom implementation of a Formatter (org.springframework.format) for MyObjectId and this is my parse method: 我为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);
}

In my spring-servlet.xml I registered my formatter: 在我的spring-servlet.xml中,我注册了格式化程序:

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

Now my problem is that when an exception is thrown from the Formatter it is not being caught by any of the @ExceptionHandler methods. 现在我的问题是,当从Formatter引发异常时,任何@ExceptionHandler方法都不会捕获该@ExceptionHandler Therefore the response is being output in this format: 因此,响应以以下格式输出:

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"]

How can I make this type of exception begin caught by an @ExceptionHandler . 如何使这种类型的异常开始被@ExceptionHandler This one of my exception handler methods: 这是我的异常处理程序方法之一:

@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);
}

I had the same situation 我有同样的情况

I don't handle the Formatter exception in the @Controller 我不处理@ControllerFormatter异常

Checking the following: 检查以下内容:

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

I suggest you work around ValidationMessages.properties (you need configure LocalValidatorFactoryBean and ReloadableResourceBundleMessageSource ) 我建议你解决ValidationMessages.properties (需要配置LocalValidatorFactoryBeanReloadableResourceBundleMessageSource

For example I have 例如我有

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

Then assuming if the wise and friendly user writes an invalid date according the formatter, my DateFormatter class, like in your case throws an exception, but since I have defined the code typeMismatch.person.dateBirth the error message is show in my web form. 然后假设明智和友好的用户是否根据格式化程序写入了无效的日期,就像您的情况一样,我的DateFormatter类会引发异常,但是由于我定义了代码typeMismatch.person.dateBirth因此错误消息会在我的Web表单中显示。

Observe our codes are similar in some way. 观察我们的代码在某种程度上是相似的。 I think Spring looks somewhere if some of the codes exists to show an appropriate message, since you have none code the exception stack trace remains, in my case how I have the code and the customized message, it is presented in my web form. 认为 Spring会在某些codes存在的地方显示适当的消息,因为您没有代码,但异常堆栈跟踪仍然保留(在我的情况下,代码和自定义消息如何显示在Web表单中)。

I didn't test yet how that customized error message should fit well through REST . 我还没有测试该定制错误消息如何通过REST很好地适合。 But in my case if some Formatter throws an error I am sure that the code exists in the ValidationMessages.properties file. 但是就我而言,如果某些Formatter引发错误,我可以确定该code存在于ValidationMessages.properties文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 @ExceptionHandler不会捕获从Spring Formatters抛出的异常 - @ExceptionHandler doesn't catch exceptions being thrown from Spring Formatters spring boot @ExceptionHandler 没有捕获@Aspect 类中抛出的异常 - spring boot @ExceptionHandler not catching exceptions throwed in @Aspect classes 捕获从Spring MVC Rest服务抛出的Jersey REST客户端的异常 - Catching exceptions from a Jersey REST client thrown from a Spring MVC Rest service @ExceptionHandler不处理抛出的异常 - @ExceptionHandler doesn't handle the thrown exceptions 从 Spring AMQP Rabbit Consumer 捕获异常 - Catching exceptions from Spring AMQP Rabbit Consumer Spring boot @ExceptionHandler 没有捕获子类异常 - Spring boot @ExceptionHandler not catching subclass exception 如何在ControllerAdvice中使用ExceptionHandler处理控制器中ExceptionHandler抛出的异常? - How to handle exception thrown from ExceptionHandler in controller with ExceptionHandler in ControllerAdvice? 捕获Spring中PropertyEditors抛出的IllegalArgumentException - Catching the IllegalArgumentException thrown by PropertyEditors in Spring 使用我创建的Exception类捕获抛出的异常 - Catching thrown exceptions with an Exception class I created 处理过滤器中ExceptionHandler抛出的异常 - Handling an exception thrown from ExceptionHandler in a filter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM