-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.")
nsv
  • 10,739
  • 8
  • 59
  • 60

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
  • 1,903
  • 1
  • 13
  • 25