SpringBoot集成Swagger的步骤

API接口文档生成工具:Swagger2这个东西,我相信大家或多或少的都有所了解。今天我们通过SpringBoot来集成它然后构建API文档。首先我们要搭建出来一个SpringBoot的环境 然后才可以进行下面的操作。

老规矩 ,先写一个Hello Word测试下环境。
在这里插入图片描述

启动项目,浏览器地址栏输入http://localhost:8080/hello
在这里插入图片描述
接着集成swagger并对它进行一些基本的配置
老规矩先导包
因为springboot我使用的是最新的 所以它的包我也使用的是最新的 它的坐标可以在maven仓库中查看

 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

写个config
在这里插入图片描述

package cn.qisui.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
    
    @Bean
    //配置swagger的Docket的bean实例
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
               // .enable(false)//是否启用swagger 如果为false 则不能在浏览器中访问
                .select()
                //配置要扫描接口的方式
                .apis(RequestHandlerSelectors.basePackage("cn.qisui.swagger.controller"))
                //过滤什么路径
               // .paths(PathSelectors.ant("/qisui/**"))
                .build();
    }
    
    //配置swagger信息
    private ApiInfo apiInfo(){

        //作者信息 这个东西是可以自定义的 
        Contact contact = new Contact("丁七岁", "https://me.csdn.net/qq_43612538", "1830944692@qq.com");
        return new ApiInfo("丁七岁的SwaggerAPI文档", 
                "长风破浪会有时,直挂云帆济沧海!", 
                "V1.0", 
                "https://me.csdn.net/qq_43612538",
                contact, 
                "Apache 2.0", 
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}

apis(RequestHandlerSelectors.basePackage(“cn.qisui.swagger.controller”))
这个是要扫描的包
.paths(PathSelectors.ant("/qisui/**"))
这个是过滤掉什么
但是如果你什么也不想弄 也是可以的它有默认的设置
来看下结果。。。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值