Swagger

目录

一:前言

二:Swagger介绍

三:使用

1:创建boot项目后导包springfox-swagger2和springfox-swagger-ui

2:配置SwaggerConfig

3:注解

4:浏览器输入此连接在线查看 http://localhost:8080/swagger-ui.html

 四:注意事项

1:在正式发布时,要关闭Swagger,出于安全考虑且节省内存空间

 2:分组


一:前言

 前后端分离,如何交互?--通过API接口(就是control上的那个@)


二:Swagger介绍


   世界上最流行的API框架
   ResultFul风格的Api文档自动生成工具(API文档和APi定义同步更新)
   直接运行,可以在线测试API接口

Swagger是一个优秀的工具,几乎所有的大公司都有使用她


三:使用


1:创建boot项目后导包springfox-swagger2和springfox-swagger-ui

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

2:配置SwaggerConfig

package com.feng.config;

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 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//点进去源码,相当于个@Component
@EnableSwagger2//开启swagger
public class SwaggerConfig {
    //配置多个分组,一个docket就是就是Swagger的bean实例(公司里你写的docket是管你的扫描的,别人是别人的,互不打扰)
    @Bean
    public Docket docket2(Environment environment) {
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");// //配置多个分组
    }

    @Bean
    public Docket docket3(Environment environment) {
        return new Docket(DocumentationType.SWAGGER_2).groupName("C");
    }


    @Bean
    public Docket docket(Environment environment) {
        //题目:我希望当前组只是在测试开发中使用,发布不使用
        /*解答:1:判断是否是生产环境
               2:注入.enable(flag)--如果当前处的环境是Dev或test就显示A组,如不是就不显示A
        * */

        //设置要显示的swagger环境,(如果当前处的环境是Dev或test就显示A组,如不是就不显示A)
        Profiles profiles = Profiles.of("dev", "test");
        //获取项目环境:
        boolean flag = environment.acceptsProfiles(profiles);//查看是否处于有dev或test环境

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //配置多个分组--该组名称
                .groupName("A")
                .enable(flag)//表示是否关闭swagger,false代表关闭,true表示开启。
                .select()
                //RequestHandlerSelectors:配置要扫描接口的方式
                //basePackage:指定要扫描的包
                //none():不扫描
                //any():扫描全部
                //withClassAnnotation:扫描类上的注解,参数是一个注解的反射对象
                //withMethodAnnotation:扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.feng.controller"))//只扫描这个包下的
                //path()-过滤什么路径--不显示什么路径
                //.paths(PathSelectors.ant("/feng/"))
                .build();

    }

    //配置swagger信息=apiInfo
    private ApiInfo apiInfo() {
        //作者信息
        Contact contact = new Contact("张海峰", "https://blog.csdn.net/weixin_45711074", "2448003093@qq.com");

        //在网页显示的swagger信息
        return new ApiInfo(
                "我的SwaggerApI文档",//swagger文档标题
                "描述什么什么项目",//项目描述
                "1.0",
                "https://www.baidu.com/",
                contact,//作者信息
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}


3:注解

config里的

@Configuration//点进去源码,相当于个@Component
@EnableSwagger2//开启swagger

Control里的

    @ApiOperation("post控制类2")
    @PostMapping("/postt2")
    public User postt2(@ApiParam("用户2") User user) {

        return user;
    }

Pojo里的

@Component
//@Api--他俩一样
@ApiModel("用户User实体类")//文档注释
public class User {
    @ApiModelProperty("用户名")
    public String username;
    @ApiModelProperty("密码")
    public String password;
}


4:浏览器输入此连接在线查看 http://localhost:8080/swagger-ui.html

              A组:(公司里你写的A组是管你的扫描的,别人是别人的,互不打扰)

                       

          B组:

                    

     浏览器点击try out进行传入参数测试

 四:注意事项

1:在正式发布时,要关闭Swagger,出于安全考虑且节省内存空间

题目:我希望当前组只是在test和dev中使用,发布不使用
        真实环境设置如下:                
          
              
  
解答:1:判断是否是生产环境
     2:注入.enable(flag)--如果当前处的环境是Dev或test就显示A组,如不是就不显示

 2:分组

     配置多个分组,一个docket就是就是Swagger的bean实例(公司里你写的docket是管你的扫描的,别人是别人的,互不打扰)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值