当数据库字段为date类型时,@ResponseBody注解在转换日期类型时会默认把日期转换为时间戳(例如: date:2017-10-25 转换为 时间戳:15003323990)。
配置
我们在使用springmvc中的 @ResponseBody 注解返回JSON时可以配置Json转换器如下:
<!-- 日期处理时间类 -->
<bean id="jsonUtils" class="com.utils.JsonUtil" lazy-init="false"/>
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" value="#{jsonUtils.mapper}"/> <!-- 用于日期输出格式转换 -->
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>*; charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->
</list>
</property>
</bean>
<!-- 日期处理时间类 -->
<bean id="jsonUtils" class="com.platform.utils.JsonUtil" lazy-init="false"/>
JsonUtil
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import java.io.IOException;
import java.lang.reflect.AnnotatedElement;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;
@Component
public class JsonUtil
{
private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static final ObjectMapper mapper;
public ObjectMapper getMapper()
{
return mapper;
}
static
{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
mapper = new ObjectMapper();
mapper.setDateFormat(dateFormat);
mapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector()
{
public Object findSerializer(Annotated a)
{
if ((a instanceof AnnotatedMethod))
{
AnnotatedElement m = a.getAnnotated();
DateTimeFormat an = (DateTimeFormat)m.getAnnotation(DateTimeFormat.class);
if ((an != null) &&
(!"yyyy-MM-dd HH:mm:ss".equals(an.pattern()))) {
return new JsonUtil.JsonDateSerializer(an.pattern());
}
}
return super.findSerializer(a);
}
});
}
public static String toJson(Object obj)
{
try
{
return mapper.writeValueAsString(obj);
}
catch (Exception e)
{
throw new RuntimeException("转换json字符失败!");
}
}
public <T> T toObject(String json, Class<T> clazz)
{
try
{
return mapper.readValue(json, clazz);
}
catch (IOException e)
{
throw new RuntimeException("将json字符转换为对象时失败!");
}
}
public static class JsonDateSerializer
extends JsonSerializer<Date>
{
private SimpleDateFormat dateFormat;
public JsonDateSerializer(String format)
{
this.dateFormat = new SimpleDateFormat(format);
}
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
throws IOException, JsonProcessingException
{
String value = this.dateFormat.format(date);
gen.writeString(value);
}
}
}