swagger使用(springboot2.3+swagger2.9.2)

1.1流行性

  • 号称世界上最流行的api框架
  • Restful api文档在线生成工具=》api文档与api定义同步更新
  • 直接运行,可以在线测试api接口
  • 支持多种语言

官网
swagger官网

1.2依赖

在项目中使用swagger需要使用springfox,依赖

  • springfox swagger2
  • springfox ui
    在这里插入图片描述

1.3springboot整合swagger

我这里用已经有的项目演示

1.3.1依赖导入

 		<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>

1.3.1swaggerConfig配置

package com.thxy.tygl.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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   //开启swagger注解
public class SwaggerConfig {

     @Bean  //配置swagger Docker bean实例
    public Docket docket(Environment environment){
        /*
            .slect().apis(RequestHandlerSelectors.basePackage("com.thxy.tygl.controller"))  扫描指定包下的接口
            .path(PathSelectors.ant("/*"))  指定符合条件的url地址接口

         */
        Profiles profile=Profiles.of("dev");     //创建开启swagger所需环境
        boolean flag=environment.acceptsProfiles(profile);   //检查环境是否是dev
//        System.out.println(flag);
        return  new Docket(DocumentationType.SWAGGER_2)
                .groupName("silence")   //配置分组信息
               .enable(flag)   //根据当前环境确定是否开启swagger
                .apiInfo(apiInfo())
                .select().apis(RequestHandlerSelectors.basePackage("com.thxy.tygl.controller")).paths(PathSelectors.any()).build()
                ;
    }

    //配置Docker的Apiinfo信息
    public ApiInfo apiInfo(){
        Contact contact=new Contact("slience","https://mp.csdn.net/console/article","1195279222@qq.com");//作者信息
        return  new ApiInfo("Silence Api Documentation",   //首页标题
                            "tygl Api Documentation",   //首页描述
                            "1.0",            //版本
                            "https://mp.csdn.net/console/article",  //团队服务器URL
                            contact ,
                            "apache2.0",
                            "",
                            new ArrayList());
    }


}

启动项目访问:
http://localhost:8080/swagger-ui.html

在这里插入图片描述
更多swagger操作请浏览此网站
swagger更多参数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值