Message parameters not resolved in localized Spring Boot validation messages

痴心易碎 提交于 2019-12-04 19:00:34

This answer helped me a lot to understand where the issue was.

The gist is that Hibernate resolved messages end up as a default message in BindingResult.

Therefore one should either:

  1. Remove braces from custom defined messages and then use:
  String msg = messageSource.getMessage(e.getDefaultMessage(), e.getArguments(), locale);

To correctly lookup the messages interpolating all arguments.

OR

  1. Give up on custom defined messages altogether and rely on Spring message codes - e.g. for the Size annotation use Size.model.field in message.properties and the code to do the lookup e.g:
String msg = messageSource.getMessage(e, locale);

My expectation was that the default message from BindingResult would have a fully interpolated text but it appears that if the message is in braces Hibernate looks up the message in the bundle but does not interpolate parameters, instead sanitizing the string and stripping off any curly braces.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!