是否可以在不使用spring框架的情况下在单独的文件中包含自定义错误消息?
我正在使用hibernate 5 +,jersey,jackson和hibernate-validator 5.2.2.Final
开发API以进行验证。我发现我需要使用自定义错误消息进行文件声明,但我不知道该做什么。
我的代码应如何通过示例显示:
public class Account {
private int id;
@NotNull(message = "{account.name.null}")
@Length(min = 1, max = 150, message = "{account.name.length}")
private String name;
@NotNull(message = "{account.lastname.null}")
@Length(min = 1, max = 150, message = "{account.lastname.length}")
private String lastName;
使用errorMessages.xml文件,结构为:
account.name.null= Name value is missing.
account.name.length= Name has to be between {min} and {max}.
我已尝试使用静态最终变量,但我无法使用方法返回消息:
public class ValidationMessages {
//ACCOUNT
public static String ACCOUNT_NAME_LENGTH(int min, int max)
{
return "Name has to be between "+min+" and "+max+".";
}
public static final String ACCOUNT_NAME_NULL = "Name value is missing.";
public class Account {
private int id;
@NotNull(message = ValidationMessages.ACCOUNT_NAME_NULL)
@Length(min = 1, max = 150 message =ValidationMessages.ACCOUNT_NAME_LENGTH(1,150))
private String name;
@NotNull(message = ValidationMessages.ACCOUNT_LASTNAME_NULL)
@Length(min = 1, max = 150 message = ValidationMessages.ACCOUNT_NAME_LENGTH(1,150))
private String lastName;
答案 0 :(得分:1)
errorMessages.xml
必须重命名为.properties
,我必须将其移至/resources
文件夹