自定义弹簧验证错误 [英] Customize spring validation error
问题描述
我要自定义弹簧验证错误
I want customize the spring validation error for
@NotNull
@Length(max = 80)
private String email;
但是我做不到. 要采取的步骤是什么?
but I'm unable to do it. What are the step to follow?
推荐答案
The JSR 303 default message interpolation algorithm allows you to customize messages by supplying a resource bundle named ValidationMessages. Create a ValidationMessages.properties
file in the classpath containing:
javax.validation.constraints.NotNull.message=CUSTOM NOT NULL MESSAGE
javax.validation.constraints.Size.message=CUSTOM SIZE MESSAGE
这会更改@Size
约束的默认消息,因此您应该使用@Size
约束,而不是特定于Hibernate的@Length
约束.
This changes the default message for the @Size
constraint, so you should use the @Size
constraint instead of the Hibernate-specific @Length
constraint.
您可以更改特定约束实例的消息,而不是更改所有约束的默认消息.在约束上设置message
属性:
Instead of changing the default message for all constraints, you can change the message for a specific constraint instance. Set the message
attribute on the constraint:
@NotNull(message = "{email.notnull}")
private String email;
并将消息添加到ValidationMessages.properties
文件:
email.notnull=E-mail address is required
这篇关于自定义弹簧验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!