Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize Spring Error Message

I want to change the default error messages being thrown by Spring.

I basically have this form. Format of the date should follow "mm/dd/yyyy"

<p>
    <label class="label">Start Date</label>
    <form:input path="dteStartDate" /> 
    <form:errors path="dteStartDate"/>
</p>

Everything works fine, but during binding I get this message added by the form:error tag.

Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property dteStartDate; nested exception is java.lang.IllegalArgumentException:

Could not parse date: Unparseable date: "2010/11/19"

Can I customize this? So that instead of these I could show something like this:

Invalid Date Format. Format should be "mm/dd/yyyy"

This is Spring MVC 2.5.

like image 429
Mark Estrada Avatar asked Jul 17 '10 04:07

Mark Estrada


People also ask

How do I create a custom exception in Spring boot?

The @ExceptionHandler is an annotation used to handle the specific exceptions and sending the custom responses to the client. Define a class that extends the RuntimeException class. You can define the @ExceptionHandler method to handle the exceptions as shown.

How do you customize the WhiteLabel error page?

We first need to create a custom HTML error page. If we save this file in resources/templates directory, it'll automatically be picked up by the default Spring Boot's BasicErrorController. We can be more specific by naming the file with the HTTP status code we want it used e.g. saving the file as 404.


1 Answers

You need to define a MessageSource to resolve error code to the message. Error codes are built as described here (so you'll have typeMismatch.<objectName>.dteStartDate).

like image 81
axtavt Avatar answered Oct 06 '22 01:10

axtavt