问题
I have a Spring MVC web application that uses JSR303 validation:
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotBlank;
class User {
@NotBlank(message = "user name is mandatory")
String userName;
public enum Color {RED, GREEN, YELLO}
@NotNull(message = "color is mandatory)
private Color color;
}
When my web controller validates User, it tells "color is mandatory" if this parameter has not been specified. This message is shown in the web form. However, if the string "BLUE" is passed to color (which is not one of the 3 enum options), I get a message as follows:
Failed to convert property value of type java.lang.String to required type User$Color for property color; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @javax.validation.constraints.NotNull User$Color for value BLUE; nested exception is java.lang.IllegalArgumentException: No enum constant User.Color.BLUE.
This message is shown in the web form. How can I personalise the message?
来源:https://stackoverflow.com/questions/24492827/how-to-personality-error-message-for-hibernate-validator-enum