springBoot整合swagger2

1.swagger是什么?有什么用?

现在大多数开发都是使用的是前后端分离,存在一个问题就是前端和后端如何更好的进行协作开发,当我们后端写好接口之后,前端就使用我们的接口拿数据就好了,但是在开发的过程中,需求如果有变化,前端不知道的话,就会导致其他不可避免的问题出现.简单的来说swagger就是一个测试我们写好接口的东西,可以配置相应的设置来达到我们的需求.

测试接口的工具有一个是 Postman,但是我们需要单独下载,使用起来也比较麻烦. 而swagger,我们只需要在项目中使用就好了,访问一个特定的地址,就可以看到我们后端到底写了哪些接口,是什么用的请求方式,以及需要什么参数.

我学习一个新的技术是这样去学习的,首先学习怎么使用,会使用之后再现象了解底层

1.先了解这个东西是什么?
2.有什么用?
3.怎么去用?
4.为什么会出现这个东西?
5.这个东西的底层是怎么样实现的?

2.快速使用(jdk8+swagger2)

1.新建一个springBoot项目并且导入以下依赖

<!--    swagger2需要的依赖    -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
       
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
    </dependencies>

2.编写swaggerConfig配置类并标准@EnableSwagger2注解

@EnableSwagger2:这个注解也很好理解,看字面意思就知道是,开启Swagger2

@Configuration
@EnableSwagger2 //开启swagger
public class SwaggerConfig {
}

3.启动springBoot项目

在浏览器访问该地址:http://localhost/swagger-ui.html#/

然后就可以看到此页面,如过成功访问到该页面,那就说明springBoot中已经成功导入了swagger2,我这里使用的是80端口,所以默认的就把端口号省略了,如果是8080端口则需要加上端口号.
在这里插入图片描述

如果遇到端口号被占用的话,使用一下解决方案(win+R 输入cmd,依次输入以下命令)


查看80端口这个进程
netstat -ano|findstr "80" 

查看80端口这个进程被那个应用所占用,手动去任务管理器查看并且结束该进程(方式1)
tasklist |findstr “进程编号” 

根据进程id结束该进程(方式2)
taskkill  /f  /t  /pid "进程编号"

3.swagger的详细配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.stereotype.Component;
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;
@Component
@Configuration
@EnableSwagger2 //开启swagger
public class SwaggerConfig {

    //配置第二个分组
    @Bean
    public Docket docket2(Environment environment) {


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("kfk") //配置分组名称
                .enable(true) //配置是否启用Swagger,如果是false,在浏览器将无法访问
                .select()// 通过.select()方法,去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
                .apis(RequestHandlerSelectors.basePackage("com.compass.swagger"))
                // 配置如何通过path过滤,即这里只扫描请求以/hello开头的接口
                .paths(PathSelectors.ant("/hello/**"))
                .build();
    }

    @Bean
    public Docket docket(Environment environment) {

        //设置需要显示的swagger环境
        Profiles of = Profiles.of("dev", "test");
        boolean flag = environment.acceptsProfiles(of);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("compass")////配置分组名称
                .enable(true) //配置是否启用Swagger,如果是false,在浏览器将无法访问
                .select()// 通过.select()方法,去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
                .apis(RequestHandlerSelectors.any())
                // 配置如何通过path过滤,即这里只扫描请求以/hello开头的接口
                .paths(PathSelectors.ant("/**"))
                .build();
    }


    private ApiInfo apiInfo() {
        Contact contact = new Contact("联系人名字", "https://www.compass.com", "compass@qq.com");
        return new ApiInfo(
                "Swagger学习", // 标题
                "学习演示如何配置Swagger", // 描述
                "v1.5", // 版本
                "http://wwweb.top/book_ctiy/", // 组织链接
                contact, // 联系人信息
                "Apach 2.0 许可", // 许可
                "许可链接", // 许可连接
                new ArrayList<>()// 扩展
        );
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值