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”的样式,输出的一直都是数字,这是问什么呀?求帮忙!!!
答
从你提供的代码来看你只是new了一个JsonDateSerializer ,并没有交给objectmapper使用,他是不会起作用的。
关于responsebody的json处理是用MappingJacksonHttpMessageConverter来处理,在这个类里面调用objectmapper
答
没有问题,可以啊!可能是你其他地方错了。
答
你的注解要写在get方法上,不是写在属性上