SpringBoot同时集成swagger与knife4j2种API文档

1.先来看看swagger与knife4j的外观

SpringBoot同时集成swagger与knife4j2种API文档_第1张图片

SpringBoot同时集成swagger与knife4j2种API文档_第2张图片

2.接下来讲如何同时配置swagger与knife4j

2.1配置pom依赖

 
        
            com.github.xiaoymin
            knife4j-spring-boot-starter
            
            2.0.4
        
        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        

2.2 配置spring config配置类

新建1个java类 

ApiDocConfig.java
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class ApiDocConfig {

    private final ServerProperties serverProperties;
    private final SysInfoProperties sysInfoProperties;

    public Knife4jConfig(ServerProperties serverProperties, SysInfoProperties sysInfoProperties) {
        this.serverProperties = serverProperties;
        this.sysInfoProperties = sysInfoProperties;
    }


    private ApiInfo apiInfo() {
        String serviceUrl = "http://localhost:" + serverProperties.getPort() +
                serverProperties.getServlet().getContextPath();
        return new ApiInfoBuilder()
                .title("xxxx服务后台接口文档")
                .description("xxxx服务")
                .termsOfServiceUrl(serviceUrl)
                .version(sysInfoProperties.getVersion())
                .contact(new Contact(sysInfoProperties.getPic(), null, "[email protected]"))
                .build();
    }

    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //分组名称
                .groupName("1.X版本接口")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.xxxxx.contentstatistics.web"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
}

3.相关的SysInfoProperties.java

@Data
@Component
// 注册为组件
@EnableConfigurationProperties
// 启用配置自动注入功能
@ConfigurationProperties(prefix = "project")
//指定类对应的配置项前缀
@ApiModel(description = "系统描述")
public class SysInfoProperties {

    @ApiModelProperty(value = "完整应用名")
    private String application;
    @ApiModelProperty(value = "应用名")
    private String name;
    @ApiModelProperty(value = "应用中文名")
    private String chineseName;
    @ApiModelProperty(value = "版本")
    private String version;
    @ApiModelProperty(value = "开发")
    private String pic;
    @ApiModelProperty(value = "框架")
    private String framework;


}

 

你可能感兴趣的:(SpringBoot技术笔记,swagger,kinfe4j,springboot)