久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Springboot中如何使用Jackson

瀏覽:2日期:2023-04-10 15:26:37

1、SpringMVC中默認(rèn)集成

SpringMVC已經(jīng)默認(rèn)集成了JackSon,如下所示:

@RequestMapping('/addUserInfo') public UserInfo addUserInfo(@RequestBody UserInfo userInfo){ }

可以用UserInfo對(duì)象來接前臺(tái)傳過來的json,SpringMVC已經(jīng)幫我們自動(dòng)反序列化。

Springboot中如何使用Jackson

可以看到,在SpringBoot中,只需要導(dǎo)入web starter,不需要添加其他的依賴,就可以使用Jackson。

2、時(shí)間格式化

在序列化的過程中,如果有Date格式,我們可以通過下面幾種方式來對(duì)時(shí)間字段進(jìn)行格式化。

2.1、注解方式

通過添加JsonFormat注解,可以固定日期格式。

public class UserInfo { private String name; private String password; private Integer age; @JsonFormat(pattern = 'yyyy-MM-dd') private Date birth;

也可以通過這個(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è)類中,原來的方法是:

@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,與之前版本的略有差異,不過可以看到,給 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)過測試,注解方式的優(yōu)先級(jí)要高于下面的兩種。

3、Jackson的簡單使用

//測試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的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 韩城市| 临夏市| 宜城市| 沂水县| 祁连县| 长丰县| 鄂尔多斯市| 天全县| 综艺| 湛江市| 平武县| 凤山市| 丁青县| 太康县| 郎溪县| 绵阳市| 曲阳县| 宝清县| 宁化县| 名山县| 四会市| 玉环县| 阳西县| 贵溪市| 湛江市| 开远市| 车致| 东源县| 吴江市| 平泉县| 通州区| 广安市| 平昌县| 宝丰县| 龙岩市| 璧山县| 密云县| 黄石市| 邻水| 扶余县| 大方县|