欢送拜访我的GitHub
https://github.com/zq2599/blog_demos
内容:所有原创文章分类汇总及配套源码,波及Java、Docker、Kubernetes、DevOPS等;
系列文章汇总
- jackson学习之一:根本信息
- jackson学习之二:jackson-core
- jackson学习之三:罕用API操作
- jackson学习之四:WRAP_ROOT_VALUE(root对象)
- jackson学习之五:JsonInclude注解
- jackson学习之六:罕用类注解
- jackson学习之七:罕用Field注解
- jackson学习之八:罕用办法注解
- jackson学习之九:springboot整合(配置文件)
- jackson学习之十(终篇):springboot整合(配置类)
本篇概览
- 本文是《jackson学习》系列的终篇,通过后面的一系列实战,置信您已能够纯熟应用jackson灵便的执行各种json序列化和反序列化操作,那么,本篇就以轻松的形式来实现整个系列吧;
- 上一篇介绍的是在springboot中通过配置文件对jackson做设置,明天要聊的是另一种罕用的jackson配置形式:<font color="blue">配置类</font>,就是本人编写代码实例化和配置springboot全局应用的ObjectMapper实例;
源码下载
- 如果您不想编码,能够在GitHub下载所有源码,地址和链接信息如下表所示(https://github.com/zq2599/blo...:
名称 | 链接 | 备注 |
---|---|---|
我的项目主页 | https://github.com/zq2599/blo... | 该我的项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blo... | 该我的项目源码的仓库地址,https协定 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该我的项目源码的仓库地址,ssh协定 |
- 这个git我的项目中有多个文件夹,本章的利用在<font color="blue">jacksondemo</font>文件夹下,如下图红框所示:
- <font color="blue">jacksondemo</font>是父子构造的工程,本篇的代码在<font color="red">springbootconfigbean</font>子工程中,如下图:
编码
- 在父工程jacksondemo下新增子工程springbootconfigbean,pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>jacksondemo</artifactId> <groupId>com.bolingcavalry</groupId> <version>1.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <groupId>com.bolingcavalry</groupId> <artifactId>springbootconfigbean</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springbootconfigbean</name> <description>Demo project for Spring Boot with Jackson, configuration from config bean</description> <properties> <java.version>1.8</java.version> </properties> <!--不必spring-boot-starter-parent作为parent时的配置--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.3.3.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!-- swagger依赖 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> </dependency> <!-- swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
- 本文最重要的代码是配置类<font color="blue">JacksonConfig.java</font>,如下,须要<font color="red">ConditionalOnMissingBean</font>注解防止抵触,另外还给实例指定了名称customizeObjectMapper,如果利用中通过Autowired应用此实例,须要指定这个名字,防止报错"There is more than one bean of 'ObjectMapper ' type":
@Configurationpublic 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办法内的设置,如果您想做更多设置,请参考《jackson学习之三:罕用API操作》外面的设置内容;
- 启动类仍然很简略:
package com.bolingcavalry.springbootconfigbean;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic 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@EnableSwagger2public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .tags(new Tag("JsonPropertySerializationController", "JsonProperty相干测试")) .select() // 以后包门路 .apis(RequestHandlerSelectors.basePackage("com.bolingcavalry.springbootconfigbean.controller")) .paths(PathSelectors.any()) .build(); } //构建 api文档的详细信息函数,留神这里的注解援用的是哪个 private ApiInfo apiInfo() { return new ApiInfoBuilder() //页面题目 .title("SpringBoot整合Jackson(基于配置文件)") //创建人 .contact(new Contact("程序员欣宸", "https://github.com/zq2599/blog_demos", "zq2599@gmail.com")) //版本号 .version("1.0") //形容 .description("API 形容") .build(); }}
- 最初是测试用的Controller类,要留神的是在应用ObjectMapper实例的中央,用Autowired注解的时候,<font color="red">记得带上Qualifier注解</font>:
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后,浏览器关上:<font color="blue">http://localhost:8080/swagger-ui.html</font>
- 先验证序列化接口/jsonproperty/serialization:
- 再验证反序列化接口 /jsonproperty/deserialization:
- 至此,整个《jackson学习》系列就全副实现了,心愿这十篇内容可能给您带来一些参考,助您在编码过程中更加得心应手的应用Jackson;
你不孤独,欣宸原创一路相伴
- Java系列
- Spring系列
- Docker系列
- kubernetes系列
- 数据库+中间件系列
- DevOps系列
欢送关注公众号:程序员欣宸
微信搜寻「程序员欣宸」,我是欣宸,期待与您一起畅游Java世界...
https://github.com/zq2599/blog_demos