5

我想在休息服务中使用路径参数作为完整的 ISO 时间戳。

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

我之前打开了 mvc:annotation 驱动,但将其关闭,因此我可以在 DefaultAnnotationHandlerMapping 上将“useDefaultSuffixPattern”设置为 false。

控制器代码

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

我收到以下错误。我可以看到框架最终使用正确的字符串调用已弃用的 Date.parse() 方法,而不是使用 joda time 来完成工作。

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

我希望 joda 解析完整的 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"/>

4

2 回答 2

0

我看到的一个可能的问题是您没有使用 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>
于 2012-10-10T00:26:06.400 回答
0

这太晚了(准确地说是 3 年),但它可能对其他人有所帮助。

该网址缺少时间指示符“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
于 2016-04-26T10:51:27.233 回答