jackson学习之九:springboot整合(配置文件)

  1. 用properties或yml配置文件来配置,即本篇的内容;

  2. 用配置类来配置,这是下一篇文章的主题;

本篇概览

今天实战内容如下:

  1. 开发springboot应用,体验springboot默认支持jackson,包括jackson注解和ObjectMapper实例的注入;

  2. 在application.yml中添加jackson配置,验证是否生效;

源码下载

  1. 如果您不想编码,可以在GitHub下载所有源码,地址和链接信息如下表所示(https://github.com/zq2599/blog_demos):

| 名称 | 链接 | 备注 |

| :-- | :-- | :-- |

| 项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |

| git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |

| git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |

  1. 这个git项目中有多个文件夹,本章的应用在jacksondemo文件夹下,如下图红框所示:

在这里插入图片描述

  1. jacksondemo是父子结构的工程,本篇的代码在springbootproperties子工程中,如下图:

在这里插入图片描述

开始实战

  1. 由于同属于《jackson学习》系列文章,因此本篇的springboot工程作为jacksondemo的子工程存在,pom.xml如下,需要注意的是parent不能使用spring-boot-starter-parent,而是通过dependencyManagement节点来引入springboot依赖:
<?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”>

4.0.0

jacksondemo

com.bolingcavalry

1.0-SNAPSHOT

…/pom.xml

com.bolingcavalry

springbootproperties

0.0.1-SNAPSHOT

springbootproperties

Demo project for Spring Boot

<java.version>1.8</java.version>

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

  1. 启动类很平常:

package com.bolingcavalry.springbootproperties;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class SpringbootpropertiesApplication {

public static void main(String[] args) {

SpringApplication.run(SpringbootpropertiesApplication.class, args);

}

}

  1. 由于用到了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();

}

}

  1. 序列化和反序列化用到的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 +

‘}’;

}

}

  1. 测试用的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;

import org.springframework.beans.factory.annotation.Autowired;

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

@Autowired

ObjectMapper mapper;

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值