springboot整合(配置类)

父工程jacksondemo下新增子工程springbootconfigbean,pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>


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

<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>
本文最重要的代码是配置类JacksonConfig.java,如下,需要ConditionalOnMissingBean注解避免冲突,另外还给实例指定了名称customizeObjectMapper,如果应用中通过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方法内的设置,如果您想做更多设置,请参考《jackson学习之三:常用API操作》里面的设置内容;
启动类依然很简单:
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相关测试"))
            .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注解的时候,记得带上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();
}

}
USB Microphone https://www.soft-voice.com/
Wooden Speakers https://www.zeshuiplatform.com/
亚马逊测评 www.yisuping.cn
深圳网站建设www.sz886.com

上一篇:swagger展示枚举类型


下一篇:SpringCloud Gateway 整合Springfox(SwaggerUI3) 之后调用某一个服务的接口时,请求路径不会加上对应的服务名问题