Spring - how to handle an exception

I am using Spring with speed. From time to time, speed throws an exception, for example

Error in string literal interpolation: org.apache.velocity.exception.MethodInvocationException: calling getMessage method in class org.springframework.web.servlet.support.RequestContext threw an exception class org.springframework.context.NoSuchMessageException: No message found under code " XX "for the locale" en_US ".

Question: Can I instruct Spring to crush NoSuchMessageException? I am new to Spring, so I don’t know if I can create an exception handler that will not throw an exception if the message cannot be found? In my use case, this is a valid use case when you do not find some messages in the file messages.properties.

[EDIT] - I found a way org.apache.velocity.app.event.MethodExceptionEventHandlerto provide an even speed handler. However, I'm not sure how to register it using Spring.

+3
source share
1 answer

It would be better, I think, to address the problem directly rather than trying to suppress the exception, which can lead to behavior and incomplete actions.

However, you did not tell us how you want the system to respond when the message is not defined for a given code. Do you need an empty string, or should the code itself be used as the default message?

, - Spring messageSource bean. messageSource beans useCodeAsDefaultMessage. false, true, , , .

messageSource ( , ) getDefaultMessage() , .

+2

Source: https://habr.com/ru/post/1769385/


All Articles