Spring Boot - 带有注释参数的自定义验证消息

时间:2017-03-07 07:57:47

标签: java spring spring-mvc spring-boot spring-validator

我想用Spring Boot / MVC Validation创建自定义验证消息。

我发现以下设置工作:

public class RegisterCredentials {

    @NotEmpty
    @NotNull
    @Size(min=3, max=15)
    private String username;

    ...

}

messages.properties:

NotEmpty.registerCredentials.username = Username field cannot be empty.
NotNull.registerCredentials.username = Username field cannot be empty.
Size.registerCredentials.username = Username length must be between 3 and 15 characters.

之后我想用属性替换固定值min = 3和max = 15。我试过这样,但它没有用(它适用于@Size(message =“..”)注释):

Size.registerCredentials.username = Username length must be between {min} and {max} characters.

然后我发现以下代码工作..:

Size.registerCredentials.username = Username length must be between {1} and {2} characters.

嗯..几乎,因为它产生以下信息:

Username length must be between 15 and 3 characters.

替换这些{1}和{2}的顺序可以解决问题,但是在进一步的分析中会产生令人困惑的代码。

使用干净的代码解决这个问题是否有解决方案?

0 个答案:

没有答案