2012-10-09 26 views
3

我想在休息服務中使用路徑參數作爲完整的ISO時間戳。無法使用DateTimeFormat註解來獲取Spring MVC解析日期時間

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

我以前有MVC:註解驅動的開啓,但把它關掉了,所以我可以「useDefaultSuffixPattern」設置爲在DefaultAnnotationHandlerMapping假。

控制器代碼

@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); 
} 

我碰到下面的錯誤。我可以看到框架最終使用正確的String調用了不推薦使用的Date.parse()方法,而不是使用joda時間來完成這項工作。

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 

我想喬達解析完整的ISO日期,如org.springframework.format.annotation.DateTimeFormat.java註釋文件中指定,像這樣:

/** 
    * 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, ..... 

應用程序上下文配置

<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"/> 

+0

你獲得你的網址完整日期時間? (因爲空格) – Luciano

+0

是的,我可以看到它的值進入Date.parse(),它是正確的。我也url編碼它,它也是這樣。 –

+0

您的userMidnightTime參數不應該是任何JodaTime類型(例如LocalDateTime)而不是Date? – Luciano

回答

0

一個可能的問題,我看到的是,你還沒有註冊的便利着想rsionService用的HandlerAdapter,你可以這樣來做:

<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> 
+0

我確實在那裏,我讀了另一個帖子,這是沒有必要的。我得到相同的結果與任何方法 –

+0

你是否能夠解決這個問題? – lives

0

這是太晚(3年,恰恰),但它可以幫助別人。

的URL是缺少時間標誌 'T',所以儘量的

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

代替

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