Springboot中如何使用Jackson
1、SpringMVC中默認(rèn)集成
SpringMVC已經(jīng)默認(rèn)集成了JackSon,如下所示:
@RequestMapping("/addUserInfo")
public UserInfo addUserInfo(@RequestBody UserInfo userInfo){
}
可以用UserInfo對(duì)象來(lái)接前臺(tái)傳過(guò)來(lái)的json,SpringMVC已經(jīng)幫我們自動(dòng)反序列化。

可以看到,在SpringBoot中,只需要導(dǎo)入web starter,不需要添加其他的依賴,就可以使用Jackson。
2、時(shí)間格式化
在序列化的過(guò)程中,如果有Date格式,我們可以通過(guò)下面幾種方式來(lái)對(duì)時(shí)間字段進(jìn)行格式化。
2.1、注解方式
通過(guò)添加JsonFormat注解,可以固定日期格式。
public class UserInfo {
private String name;
private String password;
private Integer age;
@JsonFormat(pattern = "yyyy-MM-dd")
private Date birth;
也可以通過(guò)這個(gè)注解指定時(shí)區(qū)(time zone)
2.2、重寫bean
也可以重新 JacksonHttpMessageConvertersConfiguration 類中的bean
@Configuration
public class WebMvcConfig {
@Bean
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat("yyyy/MM/dd"));
mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
return mappingJackson2HttpMessageConverter;
}
}
在JacksonHttpMessageConvertersConfiguration這個(gè)類中,原來(lái)的方法是:
@ConditionalOnClass({ObjectMapper.class})
@ConditionalOnBean({ObjectMapper.class})
@ConditionalOnProperty(
name = {"spring.mvc.converters.preferred-json-mapper"},
havingValue = "jackson",
matchIfMissing = true
)
static class MappingJackson2HttpMessageConverterConfiguration {
MappingJackson2HttpMessageConverterConfiguration() {
}
@Bean
@ConditionalOnMissingBean(
value = {MappingJackson2HttpMessageConverter.class},
ignoredType = {"org.springframework.hateoas.server.mvc.TypeConstrainedMappingJackson2HttpMessageConverter", "org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter"}
)
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
return new MappingJackson2HttpMessageConverter(objectMapper);
}
}
這是最新的版本的spring,與之前版本的略有差異,不過(guò)可以看到,給 mappingJackson2HttpMessageConverter方法注入了一個(gè)ObjectMapper,那么我們可不可以直接修改ObjectMapper呢?當(dāng)然可以,在Jackson的自動(dòng)配置類(JacksonAutoConfiguration)中,可以發(fā)現(xiàn):
@ConditionalOnClass({Jackson2ObjectMapperBuilder.class})
static class JacksonObjectMapperConfiguration {
JacksonObjectMapperConfiguration() {
}
@Bean
@Primary
@ConditionalOnMissingBean
ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
return builder.createXmlMapper(false).build();
}
}
在這個(gè)內(nèi)部類里,提供了ObjectMapper。所以我們可以直接重新這個(gè)Bean,也可以達(dá)到全局修改日期格式的作用。
@Configuration
public class WebMvcConfig {
@Bean
ObjectMapper jacksonObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
return objectMapper;
}
}
經(jīng)過(guò)測(cè)試,注解方式的優(yōu)先級(jí)要高于下面的兩種。
3、Jackson的簡(jiǎn)單使用
//測(cè)試jackSon
public static void main(String[] args) throws JsonProcessingException {
UserInfo userInfo = getTestUser();
ObjectMapper objectMapper = new ObjectMapper();
//將對(duì)象序列化為json字符串
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); //忽略為null的字段
String userJsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(userInfo);
System.out.println(userJsonString);
//將json反序列化為java對(duì)象
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
UserInfo userInfo2 = objectMapper.readValue(userJsonString, UserInfo.class);
System.out.println(userInfo2);
}
本文作者:DayRain
本文鏈接:https://www.cnblogs.com/phdeblog/p/13234842.html
以上就是Springboot中如何使用Jackson的詳細(xì)內(nèi)容,更多關(guān)于Springboot中使用Jackson的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
EL表達(dá)式的隱式對(duì)象_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了EL表達(dá)式的隱式對(duì)象,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
java開發(fā)之SQL語(yǔ)句中DATE_FORMAT函數(shù)舉例詳解
要將日期值格式化為特定格式,請(qǐng)使用DATE_FORMAT函數(shù),下面這篇文章主要給大家介紹了關(guān)于java開發(fā)之SQL語(yǔ)句中DATE_FORMAT函數(shù)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05
Java應(yīng)用程序開發(fā)學(xué)習(xí)之static關(guān)鍵字應(yīng)用
今天小編就為大家分享一篇關(guān)于Java應(yīng)用程序開發(fā)學(xué)習(xí)之static關(guān)鍵字應(yīng)用,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
MapReduce實(shí)現(xiàn)TopN效果示例解析
這篇文章主要為大家介紹了MapReduce實(shí)現(xiàn)TopN效果示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Java的ThreadContext類加載器的實(shí)現(xiàn)
這篇文章主要介紹了Java的ThreadContext類加載器的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06

