Java Spring3.1JSON日期格式

Java Spring3.1JSON日期格式,java,spring,spring-mvc,Java,Spring,Spring Mvc,我使用带注释的Spring3.1MVC代码(SpringMVC),当我通过@RequestBody发送date对象时,日期显示为数字。 这是我的控制器 @Controller @RequestMapping("/test") public class MyController { @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Date.

我使用带注释的Spring3.1MVC代码(SpringMVC),当我通过@RequestBody发送date对象时,日期显示为数字。 这是我的控制器

@Controller
@RequestMapping("/test")
public class MyController {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class,
                  new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
    }


    @RequestMapping(value = "/getdate", method = RequestMethod.GET)
    public @ResponseBody Date getDate(@RequestParam("dt") Date dt, Model model) {
        // dt is properly constructed here..
        return new Date();
    }
}
当我传入日期时,我能够以格式接收日期。 但我的浏览器将日期显示为数字

1327682374011
如何以我为webbinder注册的格式显示日期?
我在一些论坛上看到我应该使用jackson mapper,但我不能更改现有的mapper吗?

为了覆盖Jakson的默认日期格式策略,下面是要执行的步骤:

  • 扩展
    JsonSerializer
    以创建用于处理日期格式的新类
  • 重写
    serialize(Date-Date,JsonGenerator-gen,SerializerProvider-provider)
    函数以所需格式格式化日期并将其写回生成器实例(gen)
  • 使用
    @JsonSerialize(using=CustomDateSerializer.class)
  • 代码:

    //CustomDateSerializer类
    公共类CustomDateSerializer扩展JsonSerializer{
    @凌驾
    public void serialize(日期值、JsonGenerator gen、SerializerProvider arg2)抛出
    IOException,JsonProcessingException{
    SimpleDataFormat格式化程序=新的SimpleDataFormat(“yyyy-MM-dd”);
    字符串formattedDate=formatter.format(值);
    一般书面记录(格式日期);
    }
    }
    //日期获取法
    @JsonSerialize(使用=CustomDateSerializer.class)
    公共日期getDate(){
    返回日期;
    }
    

    来源:

    或者,如果您正在使用jackson,并且希望所有日期(不仅仅是您注释的日期)上都有ISO-8601日期,您可以禁用将日期写入时间戳的默认设置

    <bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper"/>
    <bean id="jacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig" factory-bean="jacksonObjectMapper" factory-method="getSerializationConfig" />
    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" ref="jacksonSerializationConfig" />
        <property name="targetMethod" value="disable" />
        <property name="arguments">
            <list>
                <value type="org.codehaus.jackson.map.SerializationConfig.Feature">WRITE_DATES_AS_TIMESTAMPS</value>
            </list>
        </property>
    </bean>
    
    
    将日期作为时间戳写入
    
    然后,如果要将日期转换为默认格式以外的其他格式,可以执行以下操作:

    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" ref="jacksonSerializationConfig" />
        <property name="targetMethod" value="setDateFormat" />
        <property name="arguments">
            <list>
              <bean class="java.text.SimpleDateFormat">
                <constructor-arg value="yyyy-MM-dd'T'HH:mm:ssZ"/>
              </bean>
            </list>
        </property>
    </bean>
    

    这里有一种更标准的配置方法,使用ISO8601日期,这是我为您的API推荐的

    <!-- you can't call it objectMapper for some reason -->
    <bean name="jacksonObjectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
        <property name="featuresToDisable">
            <array>
                <util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS"/>
            </array>
        </property>
    </bean>
    
    <!-- setup spring MVC -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper" ref="jacksonObjectMapper"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    
    
    
    

  • 谢谢Waqas。我想知道spring中默认的JSON序列化程序是如何工作的,因为类路径中没有jackson JAR。可能它没有使用jackson。@M.AtifRiaz用户自2012年10月22日18:27起未处于活动状态。:)
    <!-- you can't call it objectMapper for some reason -->
    <bean name="jacksonObjectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
        <property name="featuresToDisable">
            <array>
                <util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS"/>
            </array>
        </property>
    </bean>
    
    <!-- setup spring MVC -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper" ref="jacksonObjectMapper"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>