java - 无法使用 DateTimeFormat 注释让 Spring MVC 解析日期时间

标签 java spring rest spring-mvc jodatime

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

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

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

Controller 代码

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

最佳答案

我看到的一个可能的问题是您没有向 handlerAdapter 注册 conversionService,您可以这样做:

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

关于java - 无法使用 DateTimeFormat 注释让 Spring MVC 解析日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12806613/

相关文章:

Java - 并行数组上的 ArrayIndexOutOfBounds 错误

Java正则表达式保留特定的结束标签

java - 非法状态异常 : get field slot from row 0 col -1 failed

javascript - Loopback如何设置嵌套关系

android - 为 Android 创建多人游戏服务器的理想方法(具有非常基本图形的游戏)

java - 为什么事务在 Spring JPA 中没有回滚到所需的传播级别?

java - Spring RequestMapping java.lang.IllegalArgumentException 错误

java - 刷新 Spring ApplicationContext,而不重新初始化上下文,可能吗?

java - 网络逻辑 : Using jar in EAR file instead of domain/lib

rest - 如何通过REST请求更改更改Kibana保存的搜索(发现)?