[英]@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
我不处理
@Controller
的Formatter
异常
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
(需要配置LocalValidatorFactoryBean
和ReloadableResourceBundleMessageSource
)
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.