有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring为多个验证注释提供了相同的自定义消息

在Spring 4+框架中,是否可以对多个注释使用相同的自定义消息

在我的例子中,我想为@NotNull和@NotEmpty注释显示相同的消息

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

共 (1) 个答案

  1. # 1 楼答案

    是的,这是可能的。消息未在系统中用作标识符,因此它是可能的。我建议使用ValidationMessages.properties

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

    请参阅:https://stackoverflow.com/a/4811273/5585182以防止重复复制粘贴同一消息