jackson學(xué)習(xí)之十(終篇):springboot整合(配置類)

      網(wǎng)友投稿 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如下:

      4.0.0 jacksondemo com.bolingcavalry 1.0-SNAPSHOT ../pom.xml com.bolingcavalry springbootconfigbean 0.0.1-SNAPSHOT springbootconfigbean Demo project for Spring Boot with Jackson, configuration from config bean 1.8 org.springframework.boot spring-boot-dependencies 2.3.3.RELEASE pom import org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine io.springfox springfox-swagger2 io.springfox springfox-swagger-ui org.springframework.boot spring-boot-maven-plugin

      本文最重要的代碼是配置類

      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(); } }

      jackson學(xué)習(xí)之十(終篇):springboot整合(配置類)

      驗證

      啟動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)容。

      上一篇:多個表格匯總(一個文件夾里多個表格匯總)
      下一篇:如何刪除wps表中的重復(fù)值以及如何刪除內(nèi)容中的所有重復(fù)值
      相關(guān)文章
      亚洲AV午夜成人片| 亚洲综合激情九月婷婷 | 亚洲色成人WWW永久在线观看| 亚洲精品无码专区久久久| 午夜亚洲乱码伦小说区69堂| 67194在线午夜亚洲| 亚洲乱码中文论理电影| 久久亚洲私人国产精品| 亚洲成AV人片在| 亚洲成AV人片在线观看WWW| 久久亚洲AV午夜福利精品一区 | 亚洲精品夜夜夜妓女网| 亚洲乳大丰满中文字幕| 亚洲日本va中文字幕久久| 亚洲区小说区激情区图片区| 国内精品99亚洲免费高清| 国产91精品一区二区麻豆亚洲| 亚洲精品成人网久久久久久| 亚洲日本一区二区一本一道| 国产日韩成人亚洲丁香婷婷| 亚洲熟妇无码另类久久久| 国产精品亚洲A∨天堂不卡| 亚洲国产精品无码专区| 久久综合九九亚洲一区| 亚洲国产天堂在线观看| 亚洲欧洲日本天天堂在线观看| 亚洲人成在线精品| 亚洲日韩精品无码专区| 亚洲AV成人无码网站| 亚洲AV日韩精品一区二区三区| 国产AV日韩A∨亚洲AV电影 | 亚洲av日韩片在线观看| 在线亚洲午夜理论AV大片| 亚洲av无码专区国产乱码在线观看| 亚洲男人天堂av| 亚洲制服丝袜在线播放| 亚洲国产综合AV在线观看| 亚洲 另类 无码 在线| 亚洲综合无码AV一区二区| 久久亚洲AV无码精品色午夜麻| 亚洲国产精品免费在线观看|