您的位置:首页 > 编程语言 > Java开发

SpringMVC(28):json数据的时间日期格式问题-解决与示例

2018-02-14 15:35 1286 查看
2018年2月14日

【0】前言

     @ResponseBody把数据向前台页面以JSON格式进行数据传递的时候,日期格式默认为时间戳,需要转换为可读性的“yyyy-MM-dd”。

解决方案有两种:(1)@JSONField注解;(2)配置FastJson的消息转换器--FastJasonHttpMessageConverter;

【1】实体类User.java添加注解

注解方式:@JSONField(format="yyyy-MM-dd")

1.1 user对象的日期属性加上(@JSONField(format="yyyy-MM-dd"))进行格式化处理:

public class User {
private Integer id;
@NotNull
private String userCode;
@NotNull
private String userName;
@Length(min=6,max=10,message="userPassword长度为6-10")
@NotNull
private String userPassword;
private Integer gender;
private String birthdayString;
@DateTimeFormat(pattern="yyyy-MM-dd")
@JSONField(format="yyyy-MM-dd")
private Date birthday;
private String phone;
private String address ;
private Integer userRole;
private Integer createdBy;
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date creationDate;
private String creationDateString;
private Integer modifyBy;
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date modifyDate;
private String modifyDateString;
//idPicPath 文件上传路径
private String idPicPath;
private String workPicPath;
private String roleName;
**省略set/get/构造器**
}


1.2 优点:简单直接;缺点:硬编码、强侵入、紧耦合,并且修改麻烦,实际开发不推荐(推荐--配置FastJson的消息转换器)。

1.3 测试结果:



【2】MVC配置文件--配置FastJson的消息转换器

      在springmvc中,需要进行JSON转换时,我们通常会使用FastJson提供的FastJasonHttpMessageConverter。区别上一篇博文《SpringMVC(28):json数据的中文乱码-解决与示例》采用StringHttpMessageConverter解决了中文问题。

     使用FastJasonHttpMessageConverter的WriteDateUseDateFormat 配置使用默认日期类型(默认“yyyy-MM-dd HH-mm-ss”)。

     很容易理解:即在springmvc-servlet.xml文件添加某个bean和property!

2.1 修改springmvc-servlet.xml:

<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json</value>
</list>
</property>
<property name="features">
<list>
<!-- 输出Date的日期转换器-->
<value>WriteDateUserDateFormat</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>


解释:
(1)上述配置中,提供设置FastJsonHttpMessageConverter 的features属性,指定输出时的日期转换器 WriteDateUserDateFormat,就可以按照FastJ1son默认的日期格式进行转换输出。

(2)FastJson 是JSON处理工具包,包括序列化和反序列化两部分。它提供了强大的日期处理和识别能力,在序列化时指定格式,支持多种方式实现;反序列化也可以识别多种格式的日期。可以查看源码:FastJsonHttpMessageConverter、JSON、SerializerFeature等等。

2.2 Controller层

作以下修改,无须要将user对象转换成JSON字符串,直接把获取的user对象返回即可:

@RequestMapping(value="/view.html",method=RequestMethod.GET)
@ResponseBody
public Object userView(@RequestParam String id){
System.out.println("UserController-userView");
System.out.println("**Ajax提交的数据:view id: "+id);
User user = new User();
if(id == null || "".equals(id)){
return "nodata";
}else{
try{
user = userService.getUserById(id);
System.out.println("found user: "+user);
}catch(Exception e){
e.printStackTrace();
return "error";
}
return user;
}
}


2.3 修改结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: