简体   繁体   中英

Cannot get Spring MVC to parse date time using DateTimeFormat annotation

I want to use a path parameter as a full ISO timestamp in a rest service.

http://domain:8080/ctx/someObj/2000-10-31 01:30:00.000-05:00

I previously had mvc:annotation driven turned on, but turned it off so i could set "useDefaultSuffixPattern" to false on the DefaultAnnotationHandlerMapping.

the controller code

@RequestMapping(value = "/lwc/{userMidnightTime}", method = RequestMethod.GET)
@ResponseBody
public List<ProgramSnippetView> getLiveWebcastsWithin24HoursOfTime(@PathVariable(value = "userMidnightTime") @DateTimeFormat(iso= DATE_TIME) Date userMidnightTime) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(userMidnightTime);
    cal.add(Calendar.HOUR, 24);
    Date endTime = cal.getTime();
    return programService.getLiveWebcastSnippetsWithProductionDateInRange(userMidnightTime, endTime);
}

I get the following error. I can see that the framework is ultimately calling the deprecated Date.parse() method with the correct String, instead of using joda time to do the work.

112977 [http-apr-8080-exec-7] DEBUG org.springframework.beans.BeanUtils - No property       editor [java.util.DateEditor] found for type java.util.Date according to 'Editor' suffix convention
117225 [http-apr-8080-exec-7] DEBUG org.springframework.beans.TypeConverterDelegate - Construction via String failed for type [java.util.Date]
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.Date]: Constructor threw exception; nested exception is     java.lang.IllegalArgumentException

I want joda to parse the full ISO date, as is specified in the org.springframework.format.annotation.DateTimeFormat.java annotation file like so:

/** 
     * The most common ISO DateTime Format <code>yyyy-MM-dd'T'hh:mm:ss.SSSZ</code> e.g. 2000-10-31 01:30:00.000-05:00.
     * The default if no annotation value is specified.
     */
    DATE_TIME,  .....

App Context config

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="blah.blah"/>

<mvc:resources mapping="/resources/**" location="/resources/"/>


<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="formatterRegistrars">
        <set>
            <bean class="org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar">
                <property name="useIsoFormat" value="true"/>
            </bean>
        </set>
    </property>
</bean>


<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="order" value="0"/>
    <property name="useDefaultSuffixPattern" value="false"/>

    <!-- allows for periods in url -->
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="webBindingInitializer">
        <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <property name="validator" ref="validator"/>
        </bean>
    </property>

    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <!--<property name="writeAcceptCharset" value="false" />-->
            </bean>
            <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>


            <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
        </list>
    </property>


</bean>


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json"/>
        </map>
    </property>

    <property name="defaultViews">
        <list>
            <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
                <property name="prefixJson" value="true"/>
            </bean>
        </list>
    </property>
</bean>


<mvc:view-controller path="/" view-name="home"/>

One possible issue that I see is that you have not registered conversionService with handlerAdapter, you can do it this way:

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="webBindingInitializer">
        <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <property name="conversionService" ref="conversionService"/>
            <property name="validator" ref="validator"/>
        </bean>
    </property>

This is way too late (3 years, precisely), but it may help someone else.

The url is missing the time designator 'T', so try

http://domain:8080/ctx/someObj/2000-10-31T01:30:00.000-05:00 

instead of

http://domain:8080/ctx/someObj/2000-10-31 01:30:00.000-05:00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM