Springmvc框架-json数据格式传递过程中-将时间戳转化成标准的日期格式-@JSONField

Springmvc框架-json数据格式传递过程中-将时间戳转化成标准的日期格式-@JSONField_第1张图片

 

 在上一个小demo中,我们能够看出,其实返回的日期格式也是不对的,现在返回的是一个时间戳,并不是一个标准的日期格式。

解决办法:

第一种:在要转化的实体类中添加@JSONField注解

Springmvc框架-json数据格式传递过程中-将时间戳转化成标准的日期格式-@JSONField_第2张图片

 

 Springmvc框架-json数据格式传递过程中-将时间戳转化成标准的日期格式-@JSONField_第3张图片

 

 

第二种:配置fastjson的消息转换器,来处理日期格式的问题

springmvc-servlet.xml

 1 
 2  3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 4     xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
 5     xsi:schemaLocation="
 6         http://www.springframework.org/schema/beans
 7         http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context
 9         http://www.springframework.org/schema/context/spring-context.xsd
10         http://www.springframework.org/schema/mvc
11         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
12 
13     package="cn.smbms.controller" />
14     
15         
16         
17             class="org.springframework.http.converter.StringHttpMessageConverter">
18                 
19                 
20                     
21                         application/json;charset=UTF-8
22                     
23                 
24             
25             
26             <bean
27                 class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
28                 
29                     
30                         text/html;charset=UTF-8
31                         application/json
32                     
33                 
34                 
35                     
36                         
37                         WriteDateUseDateFormat
38                     
39                 
40             
41         
42     
43 
44     
45     
46     
47     <bean
48         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
49         
50         
51     
52 
53     
54     <bean
55         class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
56         
57             
58                 error
59             
60         
61     
62 
63     
64     65         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
66         
67         
68     
69 
70 

Springmvc框架-json数据格式传递过程中-将时间戳转化成标准的日期格式-@JSONField_第4张图片

 

 修改UserController.java 这里就不再需要将返回的实体对象转换成json了

Springmvc框架-json数据格式传递过程中-将时间戳转化成标准的日期格式-@JSONField_第5张图片

 

 运行结果:

Springmvc框架-json数据格式传递过程中-将时间戳转化成标准的日期格式-@JSONField_第6张图片

 

 观察:现在返回的时间给精确到秒了

原因:

Springmvc框架-json数据格式传递过程中-将时间戳转化成标准的日期格式-@JSONField_第7张图片

 

 解决办法:

使用@JSONField控制某个要显示的字段就可以了

Springmvc框架-json数据格式传递过程中-将时间戳转化成标准的日期格式-@JSONField_第8张图片

 

 Springmvc框架-json数据格式传递过程中-将时间戳转化成标准的日期格式-@JSONField_第9张图片

 

 

你可能感兴趣的:(Springmvc框架-json数据格式传递过程中-将时间戳转化成标准的日期格式-@JSONField)