jackson 学习之九:springboot 整合 (配置文件)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.3.RELEASE</version>
<type> 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》开源 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>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<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>
启动类很平常:
package com.bolingcavalry.springbootproperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Spring Java 开源项目【ali1024.coding.net/public/P7/Java/git】 bootpropertiesApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootpropertiesApplication.class, args);
}
}
由于用到了 swagger,因此要添加 swagger 配置:
package com.bolingcavalry.springbootproperties;
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 相关测试"))
.select()
// 当前包路径
.apis(RequestHandlerSelectors.basePackage("com.bolingcavalry.springbootproperties.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();
}
}
序列化和反序列化用到的 Bean 类,可见使用了 JsonProperty 属性来设置序列化和反序列化时的 json 属性名,field0 字段刻意没有 get 方法,是为了验证 JsonProperty 的序列化能力:
package com.bolingcavalry.springbootproperties.bean;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.Date;
@ApiModel(description = "JsonProperty 注解测试类")
public class Test {
@ApiModelProperty(value = "私有成员变量")
@JsonProperty(value = "json_field0", index = 1)
private Date field0 = new Date();
public void setField0(Date field0) {
this.field0 = field0;
}
@ApiModelProperty(value = "来自 get 方法的字符串")
@JsonProperty(value = "json_field1", index = 0)
public String getField1() {
return "111";
}
@Override
public String toString() {
return "Test{" +
"field0=" + field0 +
'}';
}
}
测试用的 Controller 代码如下,很简单只有两个接口,serialization 返回序列化结果,deserialization 接受客户端请求参数,反序列化成实例,通过 toString()来检查反序列化的结果,另外,还通过 Autowired 注解从 spring 容器中将 ObjectMapper 实例直接拿来用:
package com.bolingcavalry.springbootproperties.controller;
import com.bolingcavalry.springbootproperties.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;
读者福利
由于篇幅过长,就不展示所有面试题了,感兴趣的小伙伴
更多笔记分享
评论