如何为hibernate验证器枚举的个性错误消息?

时间:2014-06-30 14:45:53

标签: hibernate-validator

我有一个使用JSR303验证的Spring MVC Web应用程序:

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;
}

当我的Web控制器验证用户时,如果未指定此参数,则会显示“颜色是必需的”。此消息显示在Web表单中。但是,如果字符串“BLUE”传递给颜色(这不是3个枚举选项之一),我会收到如下消息:

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.

此消息显示在网络表单中。我该如何个性化消息呢?

0 个答案:

没有答案