學(xué)習(xí)筆記20170601">【PMP】學(xué)習(xí)筆記20170601
1039
2025-04-02
歡迎訪問我的GitHub
這里分類和匯總了欣宸的全部原創(chuàng)(含配套源碼):https://github.com/zq2599/blog_demos
本篇概覽
本文是《Jackson學(xué)習(xí)》系列的終篇,經(jīng)過前面的一系列實戰(zhàn),相信您已可以熟練使用Jackson靈活的執(zhí)行各種json序列化和反序列化操作,那么,本篇就以輕松的方式來完成整個系列吧;
上一篇介紹的是在springboot中通過配置文件對jackson做設(shè)置,今天要聊的是另一種常用的jackson配置方式:
配置類
,就是自己編寫代碼實例化和配置springboot全局使用的ObjectMapper實例;
源碼下載
如果您不想編碼,可以在GitHub下載所有源碼,地址和鏈接信息如下表所示(https://github.com/zq2599/blog_demos):
這個git項目中有多個文件夾,本章的應(yīng)用在
jacksondemo
文件夾下,如下圖紅框所示:
jacksondemo
是父子結(jié)構(gòu)的工程,本篇的代碼在
springbootconfigbean
子工程中,如下圖:
編碼
在父工程jacksondemo下新增子工程springbootconfigbean,pom.xml如下:
本文最重要的代碼是配置類
JacksonConfig.java
,如下,需要
ConditionalOnMissingBean
注解避免沖突,另外還給實例指定了名稱customizeObjectMapper,如果應(yīng)用中通過Autowired使用此實例,需要指定這個名字,避免報錯"There is more than one bean of 'ObjectMapper ’ type":
@Configuration public class JacksonConfig { @Bean("customizeObjectMapper") @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper getObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper mapper = builder.build(); // 日期格式 mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")); // 美化輸出 mapper.enable(SerializationFeature.INDENT_OUTPUT); return mapper; } }
對于JacksonConfig.getObjectMapper方法內(nèi)的設(shè)置,如果您想做更多設(shè)置,請參考《jackson學(xué)習(xí)之三:常用API操作》里面的設(shè)置內(nèi)容;
啟動類依然很簡單:
package com.bolingcavalry.springbootconfigbean; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootConfigBeanApplication { public static void main(String[] args) { SpringApplication.run(SpringbootConfigBeanApplication.class, args); } }
swagger配置:
package com.bolingcavalry.springbootconfigbean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.service.Tag; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .tags(new Tag("JsonPropertySerializationController", "JsonProperty相關(guān)測試")) .select() // 當(dāng)前包路徑 .apis(RequestHandlerSelectors.basePackage("com.bolingcavalry.springbootconfigbean.controller")) .paths(PathSelectors.any()) .build(); } //構(gòu)建 api文檔的詳細(xì)信息函數(shù),注意這里的注解引用的是哪個 private ApiInfo apiInfo() { return new ApiInfoBuilder() //頁面標(biāo)題 .title("SpringBoot整合Jackson(基于配置文件)") //創(chuàng)建人 .contact(new Contact("程序員欣宸", "https://github.com/zq2599/blog_demos", "zq2599@gmail.com")) //版本號 .version("1.0") //描述 .description("API 描述") .build(); } }
最后是測試用的Controller類,要注意的是在使用ObjectMapper實例的地方,用Autowired注解的時候,
記得帶上Qualifier注解
:
package com.bolingcavalry.springbootconfigbean.controller; import com.bolingcavalry.springbootconfigbean.bean.Test; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/jsonproperty") @Api(tags = {"JsonPropertySerializationController"}) public class JsonPropertySerializationController { private static final Logger logger = LoggerFactory.getLogger(JsonPropertySerializationController.class); @Qualifier("customizeObjectMapper") @Autowired ObjectMapper mapper; @ApiOperation(value = "測試序列化", notes = "測試序列化") @RequestMapping(value = "/serialization", method = RequestMethod.GET) public Test serialization() throws JsonProcessingException { Test test = new Test(); logger.info(mapper.writeValueAsString(test)); return test; } @ApiOperation(value = "測試反序列化", notes="測試反序列化") @RequestMapping(value = "/deserialization",method = RequestMethod.PUT) public String deserialization(@RequestBody Test test) { return test.toString(); } }
驗證
啟動SpringbootConfigBeanApplication后,瀏覽器打開:
http://localhost:8080/swagger-ui.html
先驗證序列化接口/jsonproperty/serialization:
再驗證反序列化接口 /jsonproperty/deserialization:
至此,整個《jackson學(xué)習(xí)》系列就全部完成了,希望這十篇內(nèi)容能夠給您帶來一些參考,助您在編碼過程中更加得心應(yīng)手的使用Jackson;
歡迎關(guān)注華為云博客:程序員欣宸
學(xué)習(xí)路上,你不孤單,欣宸原創(chuàng)一路相伴…
Java JSON Spring Boot
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。