Using Java Spring 3.0 @NumberFormat annotation

2019-07-31 01:45发布

问题:

I'm currently working on a small project trying to get Java spring validation working on a web form. It does work, however I have one input for inputting age, which I then convert to number format using this annotation, if I input letters it causes this to be displayed next to the input box when the form is submitted:

"Failed to convert property value of type java.lang.String to required type java.lang.Integer for property age; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "dasdf" from type java.lang.String to type java.lang.Integer; nested exception is java.lang.IllegalArgumentException: Unable to parse dasdf"

Is there any way to change this message, I'm sure it's simple, but have searched and can't find it.

This is the validation code currently:

@NotNull
@NumberFormat(style = Style.NUMBER)
@Min(1)        
@Max(110)        
private Integer age;

Cheers, David

回答1:

I found the answer, inputting this into my messages.properties file overrides the default error message.

typeMismatch.age=Input should be a number

I knew it would be simple, but took a long time to find the syntax.