Spring Error message for MVC-Joda date and time for invalid date

I have a joda attribute datetime

on my request object and I used @DateTimeFormat

to format the date.
But when I enter an invalid date (like day 55) it displays the whole message

Could not convert property value of type java.lang.String to required type org.joda.time.DateTime for property request.myDate; Nested Exception - org.springframework.core.convert.ConversionFailedException: Unable to convert value "55/12/2014" from type java.lang.String to enter org.joda.time.DateTime; Nested Exception - org.joda.time.IllegalFieldValueException: Unable to parse "12/55/2014": value 55 for dayOfMonth must be in range [1.31]

How can I change it to a custom post as the real post is generated by Spring.

In another SO question, the Spring mvc Joda Datetime Conversion Converter is not valid if an invalid date is asked to be recorded in message.properties. But I cannot figure out how it will fetch the message from the properties file as I am not doing anything of the type as the message will be identified.



error.rejectValue

+2


source to share


1 answer


The approach you are linked with in your question should work. But the message key you should use is, for example typeMismatch.org.joda.time.DateTime

.

Even if you don't manually override a value anywhere, Spring will automatically know where to look for a message based on the rules described in the JavaDoc DefaultMessageCodesResolver

.

In the situation you described, Spring will look for the following codes:



  • typeMismatch.request.myDate

    - conversion message for an attribute named "myDate" for an object named "request"
  • typeMismatch.myDate

    - Conversion message for attribute named myDate
  • typeMismatch.org.joda.time.DateTime

    - Conversion message for DateTime type
  • typeMismatch

    - General conversion error message

This way you can define some general message typeMismatch

like Bad Format and then define more specific messages as needed. More specific error codes take precedence over the ones below. So if you have both posts typeMismatch

and typeMismatch.org.joda.time.DateTime

then the latter will be used.

+3


source







All Articles