我在Spring MVC項目(Spring Boot 1.4.2)的表單對象中使用了javax.validation.constraints.AssertTrue
註解。Spring驗證@AssertTrue自定義錯誤代碼/消息
我的班級與此類似:
public class CommandForm {
@NotEmpty
@Email
private String email;
// ...
@AssertTrue(message="{error.my.custom.message}")
public boolean isValid(){
// validate fields
}
}
方法isValid
正確調用和驗證過程中正常工作,但我的自定義錯誤代碼沒有被正確解析。
我在我的message.properties
文件中有error.my.custom.message
字段,但是當驗證失敗時,我將"{error.my.custom.message}"
字符串作爲錯誤消息而不是解析的消息。
我的代碼有什麼問題?這是設置自定義錯誤代碼的正確語法嗎?