springboot集成swagger3以及美化调试

实现效果如下:

http://localhost:9999/doc.html#/

http://localhost:9999/swagger-ui/index.html#/

代码开始:

一 引入pom

<!-- swagger接口文档 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>
<!-- 引入swagger-bootstrap-ui包,优化UI页面,可不加 -->
<dependency>
     <groupId>com.github.xiaoymin</groupId>
      <artifactId>swagger-bootstrap-ui</artifactId>
      <version>1.8.5</version>
</dependency>

二 新建配置类

SwaggerConfig.java,代码如下:

package com.example.ytyproject.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
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.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@EnableOpenApi//会自动开启配置,启动类不需要加任何注解
@EnableSwaggerBootstrapUI//访问美化,方便查看调试
public class SwaggerConfig {

    @Bean
    public Docket createRestApi(){
        //Docket: 摘要对象,通过对象配置 描述文件的信息
        Docket docket = new Docket(DocumentationType.OAS_30);
        docket.apiInfo(myApiInfo())
                //select():返回ApiSelectorBuilder对象,通过对象调用build()可以创建Docket对象
                .select()
                // 指定要扫描/维护接口文档的包(否则就全部扫描)
                .apis(RequestHandlerSelectors.basePackage("com.example.ytyproject.controller"))
                // 路径过滤:该Docket-UI展示时,只展示指定路径下的接口文档(any表示都展示)
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    // 接口文档的概要信息,返回ApiInfo对象
    private ApiInfo myApiInfo(){
        //标题
        String title = "电商管理平台接口文档";
        //简单描述
        String description = "这是一个测试的描述,请检查";
        //版本
        String version = "V1.0.0";
        // url接口路径前缀
        String termsOfServiceUrl = "/";
        //作者信息
        Contact contact = new Contact("SLIU","","123456@qq.com");
        //协议
        String license = "The Apache License";
        //协议url
        String licenseUrl = "https://wwww.baidu.com";

        ApiInfo apiInfo =  new ApiInfoBuilder()
                .title(title)
                .description(description)
                .version(version)
                .termsOfServiceUrl(termsOfServiceUrl)
                .contact(contact)
                .license(license)
                .licenseUrl(licenseUrl)
                .build();
        return apiInfo;
    }

}

三 代码实现

集成完毕


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天雨编程

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

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

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

打赏作者

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

抵扣说明:

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

余额充值