简体   繁体   English

Spring - 多个验证注释的相同自定义消息

[英]Spring - same custom message for multiple validation annotations

Is it possible to use the same custom message for multiple annotations in Spring 4+ framework.是否可以在 Spring 4+ 框架中对多个注释使用相同的自定义消息。

In my case, I would like to show the same message for @NotNull and @NotEmpty annotations.就我而言,我想为 @NotNull 和 @NotEmpty 注释显示相同的消息。

@NotEmpty(message = "First name cannot be empty.")
@NotNull(message = "First name cannot be empty.")

Yes, this is possible.是的,这是可能的。 Message is not used as an identifier in the system so it is possible.消息在系统中不用作标识符,因此是可能的。 I would suggest to use ValidationMessages.properties and我建议使用ValidationMessages.properties

@NotEmpty(message = "{first.notnullorempty}")
@NotNull(message = "{first.notnullorempty}")

See: https://stackoverflow.com/a/4811273/5585182 to prevent copy pasting the same message over and over again.请参阅: https : //stackoverflow.com/a/4811273/5585182以防止一遍又一遍地复制粘贴相同的消息。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM