这里分类和汇总了欣宸的全部原创(含配套源码):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协议 |
<?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>springbootproperties</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootproperties</name>
<description>Demo project for Spring Boot</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>
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);
}
}
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();
}
}
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 +
‘}‘;
}
}
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;
@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();
}
}
spring:
jackson:
# 日期格式化
date-format: yyyy-MM-dd HH:mm:ss
# 序列化相关
serialization:
# 格式化输出
indent_output: true
# 忽略无法转换的对象
fail_on_empty_beans: true
# 反序列化相关
deserialization:
# 解析json时,遇到不存在的属性就忽略
fail_on_unknown_properties: false
# 设置空如何序列化
defaultPropertyInclusion: NON_EMPTY
parser:
# 允许特殊和转义符
allow_unquoted_control_chars: true
# 允许单引号
allow_single_quotes: true
4. 重新运行springboot应用,用浏览器访问:http://localhost:8080/jsonproperty/serialization ,结果如下图,可见json_field0的格式变成了yyyy-MM-dd HH:mm:ss,而且json输出也做了格式化,证明application.yml中的配置已经生效:
5. 再来试试反序列化,打开swagger页面,操作和响应如下图所示,注意红框1里面请求参数的格式:
微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界...
https://github.com/zq2599/blog_demos
jackson学习之九:springboot整合(配置文件)
原文:https://www.cnblogs.com/bolingcavalry/p/14383800.html