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
@Configurationpublic 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á)到全局修改日期格式的作用。
@Configurationpublic 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)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. HTML DOM setInterval和clearInterval方法案例詳解2. 使用css實(shí)現(xiàn)全兼容tooltip提示框3. 詳解CSS偽元素的妙用單標(biāo)簽之美4. 詳解PHP實(shí)現(xiàn)HTTP服務(wù)器過(guò)程5. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案6. html小技巧之td,div標(biāo)簽里內(nèi)容不換行7. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)8. XML入門的常見問(wèn)題(一)9. css進(jìn)階學(xué)習(xí) 選擇符10. ASP基礎(chǔ)入門第八篇(ASP內(nèi)建對(duì)象Application和Session)
