springboot的swagger使用

2022-06-30 08:37:41

**

springboot的swagger使用

**

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

首先导入着pom文件

在这里插入图片描述

@Configuration
@EnableOpenApi

记得要加上这个注解

适用于springfox 3.0 以上的
以下是 config中的代码

  //配置swagger 信息apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("李少","http://localhost:8080/","854549294@qq.com");
        return new ApiInfo("李少的SwaggerApi日记",
                "即使再小的帆也能远航",
                "VV1.0",
                "www.baidu.com",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                 new ArrayList());
    }

在这里插入图片描述

@Bean
    public Docket docket(Environment environment){
        //设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev");
        //通过environment。acceptsProfiles判断是否处在自己的设定的环境中
        boolean b = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(b)
                .select()
                /**
                 * RequestHandlerSelectors,配置要扫描接口的方式
                 * basePackage:指定要扫描的包
                 * any:扫描全部
                 * none:不扫描
                 * withClassAnnotation:扫描类上的注解,参数是一个注解的反射对象
                 * withMethodAnnotation:扫描方法上的注解
                 */
                .apis(RequestHandlerSelectors.basePackage("com.lishao.controller"))
                //paths 过滤(忽略)什么路径
                //.paths(PathSelectors.ant("/lishao/**"))
                .build();
    }

在这里插入图片描述
在这里插入图片描述
配置三个application在这里插入图片描述
在这里插入图片描述
在主application程序中
在这里插入图片描述
在这里插入图片描述
因为写了这个dev 所以只有启动dev的端口号才能扫描到下面的包
在这里插入图片描述

别的端口号访问的话 就不能访问
在这里插入图片描述

  • 作者:李丨少
  • 原文链接:https://blog.csdn.net/u013313232/article/details/120059572
    更新时间:2022-06-30 08:37:41