3
我正在寻找一些正确的方法来使用验证消息中的属性名称,如{min}
或{regexp}
。javax.validation如何在验证消息中获取属性名称
我已经GOOGLE了这个问题几次,并且显然没有一个本地方法来做到这一点。
@NotNull(message = "The property {propertyName} may not be null.")
private String property;
有人能帮助我吗?
更新1
使用自定义消息插值应该是这样的:
public class CustomMessageInterpolator implements MessageInterpolator {
@Override
public String interpolate(String templateString, Context cntxt) {
return templateString.replace("{propertyName}", getPropertyName(cntxt));
}
@Override
public String interpolate(String templateString, Context cntxt, Locale locale) {
return templateString.replace("{propertyName}", getPropertyName(cntxt));
}
private String getPropertyName(Context cntxt) {
//TODO:
return "";
}
}
你总是可以编写检查空值,并采取某些属性的自定义注释。 –
当然,我想过这个,但不能肯定是否是最好的选择。一旦是不是检查一个新的注释null值。我正在考虑创建通用消息的一些方法,以防止为每个属性创建一组验证消息。我已阅读了MessageInterpolator接口,但仍不知如何使用它。 –