Let say i have this java bean,
public class MyBean implements Serializable {
private Integer id;
private String userid;
private Date contacttime;
// other setter and getter
And i have a SpringMVC Controller like this, using @ModelAttribute as the method’s parameter,
@RequestMapping(value = "/doSomething", method = RequestMethod.POST)
public @ResponseBody Object doSomething(@ModelAttribute MyBean myBean) {
// do something
}
Im sending HTTP Post which format is like this
id=2& userid=3abc& contacttime=130314101010
But it shows error on my server side, which looks like this,
Failed to convert from type java.lang.String to type java.util.Date for value '130314101010'; nested exception is java.lang.IllegalArgumentException
Usually, i have to manually use SimpleDateFormat to parse my “ddMMyyHHmmss” String into Date, but SpringMVC provide a very elegant way of handling with this kind of conversion. Just by using @DateTimeFormat
import org.springframework.format.annotation.DateTimeFormat;
public class MyBean implements Serializable {
private Integer id;
private String userid;
@DateTimeFormat(pattern = "ddMMyyHHmmss")
private Date contacttime;
// other setter and getter
Dont forget to include JodaTime on your pom.xml
<dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.3</version> </dependency>
If not, you will find this kind of error,
org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.springframework.format.annotation.DateTimeFormat java.util.Date for value '130314101010'; nested exception is java.lang.IllegalStateException: JodaTime library not available - @DateTimeFormat not supported
Have fun with SpringMVC 😀
Nice and concise post, Edwin. This is what I already knew from the Spring mvc documentation and I am glad you confirmed it.
And yet I keep getting this error:
Field error in object ‘filter’ on field ‘expiredDateFrom’: rejected value [12.04.2014 11:20]; codes [typeMismatch.filter.expiredDateFrom,typeMismatch.expiredDateFrom,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [filter.expiredDateFrom,expiredDateFrom]; arguments []; default message [expiredDateFrom]]; default message [Failed to convert property value of type ‘java.lang.String’ to required type ‘java.util.Date’ for property ‘expiredDateFrom’; nested exception is java.lang.IllegalStateException: Cannot convert value of type to required type for property ‘expiredDateFrom’: no matching editors or conversion strategy found]
It seems the formatting annotations are completely ignored, I tried several datetime formats, nothing ever worked.
My setting is:
mvc portlet in Liferay, spring framework 4.0.2 release, Tomcat.
I have the required spring configuration entries including:
–
–
and I have joda library in classpath (through pom file)
The Date field annotation for the field with the above error: @DateTimeFormat(pattern=”dd.MM.yyyy HH:mm”)
Do you have any idea what else I should check?
hi Eduard Gresak,
“no matching editors or conversion strategy found” means that SpringMVC couldnt read your annotation. Perhaps you missed something? I cannot tell since i couldnt see your complete sourcecode. Sorry i cant do much help 🙁
You saved my day! Thank you man!
glad it could help 😀
its very useful. thanks edwin.
JodaDate time will not work with spring data jpa.
hi Vivek, thank you for your information 🙂