简体   繁体   中英

Spring - same custom message for multiple validation annotations

Is it possible to use the same custom message for multiple annotations in Spring 4+ framework.

In my case, I would like to show the same message for @NotNull and @NotEmpty annotations.

@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

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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