wuzhengfei 2012-02-15 23:01
浏览 344
已采纳

Spring MVC来返回Json数据时,希望格式化输出其中的日期的问题

我使用Spring MVC来返回Json数据时,希望格式化输出其中的日期,但是一直都有问题:输出的一直都是数字,不能正常格式化。

Spring的版本是:3.0.3, Jackson JSON的版本是:1.9.4

 

以下是我的代码: Spring MVC.xml文件配置如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 组件扫描,扫描包com.springmvc.controller的过程中如果发现注解,根据注解类型判断是何种类型的组件 -->
    <context:component-scan base-package="wzf.investment.common.controller"></context:component-scan>
    <context:component-scan base-package="wzf.investment.stock.controller"></context:component-scan>
    
    <mvc:annotation-driven/>
    
    <bean id="internalResourceViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass">
            <value>org.springframework.web.servlet.view.JstlView</value>
        </property>
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

实体类为:

 

import java.util.Date;

import org.codehaus.jackson.map.annotate.JsonSerialize;

import wzf.investment.base.component.JsonDateSerializer;
import wzf.investment.base.entity.BaseEntity;

public class JobSchedule extends BaseEntity{
    private static final long serialVersionUID = 1L;

    private Date startTime;
    private Date endTime;

    
    @JsonSerialize(using = JsonDateSerializer.class) 
    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    @JsonSerialize(using = JsonDateSerializer.class) 
    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }
}

格式化日期的代码为:

 

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import org.springframework.stereotype.Component;

@Component
public class JsonDateSerializer extends JsonSerializer<Date> {
    
    @Override
    public void serialize(Date value, JsonGenerator jgen,
            SerializerProvider provider) throws IOException,
            JsonProcessingException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = formatter.format(value);
        jgen.writeString(formattedDate);
    }
}

控制器中的代码为:

 

@RequestMapping("/list")
    //可处理带请求参数的action
    public @ResponseBody JobSchedule grid(){
        JobSchedule jobSchedule = new JobSchedule();
        jobSchedule.setStartTime(new Date());
        jobSchedule.setEndTime(new Date());
        return jobSchedule;
    }

访问的结果一直都没能够将日期格式化成“yyyy-MM-dd”的样式,输出的一直都是数字,这是问什么呀?求帮忙!!!

  • 写回答

3条回答

  • tianshilang 2012-02-16 10:46
    关注

    从你提供的代码来看你只是new了一个JsonDateSerializer ,并没有交给objectmapper使用,他是不会起作用的。
    关于responsebody的json处理是用MappingJacksonHttpMessageConverter来处理,在这个类里面调用objectmapper

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable