How to personality error message for hibernate validator enum?

不羁的心 提交于 2019-12-25 16:49:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!