-1

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.")
Sarvar Nishonboyev
  • 12,262
  • 10
  • 69
  • 70

1 Answers1

2

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.

Albert Bos
  • 2,012
  • 1
  • 15
  • 26