I'm using Spring 3.0.5-RELEASE, with JSR-303 style validation and Hibernate validator 4.1.0-Final. My model class looks something like this:
public class Model {
@Max(value=10,message="give a lower value")
Integer n;
}
And this is passed as request param in a spring mvc servlet with binding. So the request would look something like this:
http://localhost:8080/path?n=10
What I want is to be able to customize the error message when there is a type mismatch exception, e.g.
http://localhost:8080/path?n=somestring
Which results in a very long default message that I want to replace.
I've tried just about every configuration described on the web and none of them seem to work. Does someone know what the right configuration is?
Specifically, what do I need in my mvc-servlet.xml? What do I need in my messages.properties file? Does the message.properties file have a magic name so that hibernate-validator will find it?
I've used the following in my mvc-servlet.xml without success:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource" p:basename="messages" />
And a messages.properties file at src/main/resources (and also at src/main/webapp/WEB-INF)...
I've tried all sorts of combinations in messages.properties even for doing simple override of say @NotEmpty messages and even that doesn't work for me.
Error messages need to go into a file named ValidationMessages.properties
. Just put that somewhere in your classpath before the hibernate jars.
For example if you have a ValidationMessages.properties
file that contains the following properties:
email_invalid_error_message=Sorry you entered an invalid email address
email_blank_error_message=Please enter an email address
Then you could have a domain object with the following constraints:
public class DomainObject{
...
@Email(message = "{email_invalid_error_message}")
@NotNull(message = "{email_blank_error_essage}")
@Column(unique = true, nullable = false)
String primaryEmail;
...
}
I found an answer to this in another question and it worked for me: spring 3.0 MVC seems to be ignoring messages.properties
The confusing part to me, as I am new to Spring, is that I expected to be able to put the message in ValidationMessages.properties. However, this error happens at binding time, before validation. So instead of using ValidationMessages.properties, use your regular ResourceBundle messages.properties file.
Put this in your ??-servlet.xml file:
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"/> </bean>
Put lines like these in your messages.properties file.
typeMismatch.myRequest.amount=Amount should be a number.
methodInvocation.myRequest.amount=Amount should be a number.
In this example myRequest is my form model and amount is a property in that form. typeMismatch and methodInvocation are pre-defined and correspond to binding/conversion exceptions like the one you seem to be getting. I had a hard time finding good documentation for them or a list of values, but I found that they correspond to ERROR_CODE constants in some Spring exceptions. http://static.springsource.org/spring/docs/2.5.x/api/constant-values.html#org.springframework.beans.MethodInvocationException.ERROR_CODE
I put my messages.properties file for this project in the root of my source folder, which puts it in the root of my classpath, along with ValidationMessages.properties.
What I want is to be able to customize the error message when there is a type mismatch exception, e.g.
As I understand this messages generated by Spring MVC, not by Hibernate Validator. I suggest to add following line to the messages.properties
file:
typeMismatch.java.lang.Integer=Must specify an integer value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With