返回Json時格式化日期Date
第一步:創(chuàng)建CustomObjectMapper類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/** * 解決SpringMVC使用@ResponseBody返回json時,日期格式默認顯示為時間戳的問題。需配合<mvc:message-converters>使用 */ @Component ( "customObjectMapper" ) public class CustomObjectMapper extends ObjectMapper { public CustomObjectMapper() { CustomSerializerFactory factory = new CustomSerializerFactory(); factory.addGenericMapping(Date. class , new JsonSerializer<Date>() { @Override public void serialize(Date value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException { SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); jsonGenerator.writeString(sdf.format(value)); } }); this .setSerializerFactory(factory); } } |
第二步:配置如下:
1
2
3
4
5
6
7
|
< mvc:annotation-driven > < mvc:message-converters > < bean class = "org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" > < property name = "objectMapper" ref = "customObjectMapper" ></ property > </ bean > </ mvc:message-converters > </ mvc:annotation-driven > |
效果如下:
格式化前
格式化后
進階:返回自定義格式日期
使用@ResponseBody時返回json字符串的日期格式。Date類型屬性默認返回一個Long型的時間戳,怎樣能夠返回自定義的日期格式?
解決方案:目前有兩種方式實現(xiàn),
1、局部修改(網(wǎng)上較多,但不推薦);
繼承Jackson的抽象類:JsonSerializer<T>,然后在javabean的屬性getter()上添加注解@JsonSerialize即可實現(xiàn)。
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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; /** * @description 自定義返回JSON 數(shù)據(jù)格中日期格式化處理 */ public class CustomDateSerializer extends JsonSerializer<Date> { @Override public void serialize(Date value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException { SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); jsonGenerator.writeString(sdf.format(value)); } } |
使用方式:
1
2
3
4
|
@JsonSerialize (using = CustomDateSerializer. class ) public Date getCreateDate() { return createDate; } |
2、全局修改(強烈推薦):
MappingJacksonHttpMessageConverter主要通過ObjectMapper來實現(xiàn)返回json字符串。這里我們繼承該類,注冊一個JsonSerializer<T>。然后在配置文件中注入自定義的ObjectMapper。
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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.ObjectMapper; import org.codehaus.jackson.map.SerializerProvider; import org.codehaus.jackson.map.ser.CustomSerializerFactory; /** * @description 解決Date類型返回json格式為自定義格式 */ public class CustomObjectMapper extends ObjectMapper { public CustomObjectMapper(){ CustomSerializerFactory factory = new CustomSerializerFactory(); factory.addGenericMapping(Date. class , new JsonSerializer<Date>(){ @Override public void serialize(Date value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException { SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); jsonGenerator.writeString(sdf.format(value)); } }); this .setSerializerFactory(factory); } } |
spring-servlet.xml中配置:
1
2
3
4
5
6
7
8
|
< mvc:annotation-driven > < mvc:message-converters > < bean class = "org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" > < property name = "objectMapper" ref = "customObjectMapper" ></ property > </ bean > </ mvc:message-converters > </ mvc:annotation-driven > < bean id = "customObjectMapper" class = "com.pmc.dwa.common.custom.CustomObjectMapper" ></ bean > |