Jackson序列化(1)— [SpringBoot2.x]-Jackson在HttpMessageConverter(消息轉(zhuǎn)換器)中的使用
Jackson序列化(2)— [SpringBoot2.x]-Spring容器中ObjectMapper配置
Jackson序列化(3)— Jackson中ObjectMapper配置詳解
Jackson序列化(4)— Jackson“默認(rèn)的”時(shí)間格式化類—StdDateFormat解析
Jackson序列化(5) — Jackson的ObjectMapper.DefaultTyping.NON_FINAL屬性
Jackson序列化(6)— Java使用Jackson進(jìn)行序列化
SpringMVC的HTTP序列化和反序列化核心是HttpMessageConverter
,在SSM項(xiàng)目中搓幌,我們需要在xml配置文件中注入MappingJackson2HttpMessageConverter
。告訴SpringMVC我們需要JSON格式的轉(zhuǎn)換蚤认。
在SpringMVC中配置消息轉(zhuǎn)換器和三方消息轉(zhuǎn)換器如代碼1所示:
代碼1:SpringMVC中配置MappingJackson2HttpMessageConverter
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list >
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
我們使用的@RequestBody
和@ResponseBody
注解玫荣,他們的作用就是將報(bào)文反序列化/序列化POJO對(duì)象极景。在請(qǐng)求時(shí)SpringMVC
會(huì)在請(qǐng)求頭中尋找contentType
參數(shù)粉怕,然后去匹配能夠處理這種類型的消息轉(zhuǎn)換器。而在返回?cái)?shù)據(jù)時(shí)柳恐,SpringMVC
根據(jù)請(qǐng)求頭的Accept
屬性,再將對(duì)象轉(zhuǎn)換成響應(yīng)報(bào)文热幔。
針對(duì)于JSON這個(gè)結(jié)構(gòu)乐设,Spring默認(rèn)使用Jackson來進(jìn)行序列化和反序列化。在SpringBoot中會(huì)將MappingJackson2HttpMessageConverter
自動(dòng)裝載到IOC容器中绎巨。
源碼:org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
@Configuration
@ConditionalOnClass(ObjectMapper.class)
@ConditionalOnBean(ObjectMapper.class)
@ConditionalOnProperty(name = HttpMessageConvertersAutoConfiguration.PREFERRED_MAPPER_PROPERTY,
havingValue = "jackson", matchIfMissing = true)
protected static class MappingJackson2HttpMessageConverterConfiguration {
@Bean
@ConditionalOnMissingBean(value = MappingJackson2HttpMessageConverter.class,
ignoredType = { "org.springframework.hateoas.mvc.TypeConstrainedMappingJackson2HttpMessageConverter",
"org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter" })
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
return new MappingJackson2HttpMessageConverter(objectMapper);
}
}
而ObjectMapper
參數(shù)是Spring容器中的bean缝其,當(dāng)我們使用默認(rèn)的配置對(duì)/testDate
進(jìn)行http訪問時(shí)逢捺。
測(cè)試代碼
@RequestMapping("testDate")
@ResponseBody
public User getDateAndLocalDateTime(){
User user=new User();
user.setDate(new Date());
user.setLocalDateTime(LocalDateTime.now());
user.setUId("001");
return user;
}
/**
* 靜態(tài)內(nèi)部類鸳劳,只在該類中使用
*/
@Data
static class User{
private String uId;
private Date date;
private LocalDateTime localDateTime;
}
由上圖所示编兄,返回參數(shù)通過@ResponseBody
注解進(jìn)行了消息轉(zhuǎn)換
最終轉(zhuǎn)化為了JSON串。實(shí)際上是借助了MappingJackson2HttpMessageConverter
來完成的和媳。
2. 自定義輸出響應(yīng)內(nèi)容
MappingJackson2HttpMessageConverter
借助的是Jackson來完成序列化舶沛,那么若是可以修改Jackson
的配置,便可自定義輸出響應(yīng)內(nèi)容窗价。
對(duì)于Date或者LocalDateTime類型,我們希望按照yyyy-MM-dd HH:mm:ss
格式輸出叹卷。
借著我們對(duì)ObjectMapper
進(jìn)行功能加強(qiáng)(設(shè)置時(shí)間類序列化格式)撼港。注意:該段代碼并未覆蓋SpringBoot自動(dòng)裝配的ObjectMapper
對(duì)象,而是加強(qiáng)其配置骤竹。詳情請(qǐng)參考——SpringBoot2.x下的ObjectMapper配置原理
@Bean
public Jackson2ObjectMapperBuilderCustomizer customJackson() {
return jacksonObjectMapperBuilder -> {
//若POJO對(duì)象的屬性值為null帝牡,序列化時(shí)不進(jìn)行顯示
jacksonObjectMapperBuilder.serializationInclusion(JsonInclude.Include.NON_NULL);
//針對(duì)于Date類型,文本格式化
jacksonObjectMapperBuilder.simpleDateFormat("yyyy-MM-dd HH:mm:ss");
//針對(duì)于JDK新時(shí)間類蒙揣。序列化時(shí)帶有T的問題靶溜,自定義格式化字符串
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
jacksonObjectMapperBuilder.modules(javaTimeModule);
};
}
再次請(qǐng)求,可以看到,得到的結(jié)果如下圖所示罩息。我們已經(jīng)改變@ResponseBody對(duì)返回對(duì)象序列化的格式輸出嗤详。
文章參考
springboot學(xué)習(xí)(三)——使用HttpMessageConverter進(jìn)行http序列化和反序列化