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

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

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

2.1配置pom依赖

 <!--  knife4j版接口文档 访问/doc.html      -->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <!--在引用时请在maven中央仓库搜索最新版本号-->
            <version>2.0.4</version>
        </dependency>
        <!--  swagger 版接口文档 访问/swagger-ui.html     -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

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, "wq6e54@123.com"))
                .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;


}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ThinkPet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值