springboot2.6x 中使用Swagger3

springboot2.6x 中使用Swagger3

前言

之前我们讲解了jdk17使用swagger,本次讲解的是低版本的时候使用swagger3

效果

1、相关依赖

java 复制代码
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>

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

2、设置配置文件

不设置配置文件会报错,报错的解决方案请看 SpringBoot2.6x 使用swagger3报错:Failed to start bean 'documentationPluginsBootstrapper'

3、设置swagger 配置文件

java 复制代码
package com.example.kauyuan_classroom2.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import springfox.documentation.builders.ApiInfoBuilder;
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;


@Configuration // 标明是配置类
@EnableSwagger2 //开启swagger功能
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)  // DocumentationType.SWAGGER_2 固定的,代表swagger2
				//.groupName("分布式任务系统") // 如果配置多个文档的时候,那么需要配置groupName来分组标识
                .apiInfo(apiInfo()) // 用于生成API信息
                .select() // select()函数返回一个ApiSelectorBuilder实例,用来控制接口被swagger做成文档
                // 扫描指定包下的接口,最为常用
                .apis(RequestHandlerSelectors.basePackage("com.example.kauyuan_classroom2.controller"))
                //.withClassAnnotation(RestController.class) // 扫描带有指定注解的类下所有接口
                //.withMethodAnnotation(PostMapping.class) // 扫描带有指定注解的方法接口
                //.apis(RequestHandlerSelectors.any()) // 扫描所有
                
                // 选择所有的API,如果你想只为部分API生成文档,可以配置这里
                .paths(PathSelectors.any()
                	//.any() // 满足条件的路径,该断言总为true
                    //.none() // 不满足条件的路径,该断言总为false(可用于生成环境屏蔽 swagger)
                    //.ant("/user/**") // 满足字符串表达式路径
                    //.regex("") // 符合正则的路径
				)
                .build();
    }

    /**
     * 用于定义API主界面的信息,比如可以声明所有的API的总标题、描述、版本
     * @return
     */
    private ApiInfo apiInfo() {

        Contact contact = new Contact(
                "作者:冯浩", // 作者姓名
                "https://blog.csdn.net/m0_50207524?spm=1010.2135.3001.5343", // 作者网址
                "2236270204@qq.com"); // 作者邮箱

        return new ApiInfoBuilder()
                .title("开元教育API") //  可以用来自定义API的主标题
                .description("开元教育相关的人员管理系统的api") // 可以用来描述整体的API
                .version("1.0") // 可以用来定义版本
                .contact(contact)
                .build(); //
    }
}

该路径为项目路径

运行项目访问http://localhost:8087/swagger-ui/index.html

端口需要和设置中的端口对应上

相关推荐
找不到、了8 分钟前
栈帧四要素:JVM 方法执行的完整上下文
java·jvm
程序员小假8 分钟前
我们来说一说 Redis IO 多路复用模型
java·后端
okseekw11 分钟前
一篇吃透函数式编程:Lambda表达式与方法引用
java·后端
程序员根根11 分钟前
JavaSE 进阶:IO 流核心知识点(字节流 vs 字符流 + 缓冲流优化 + 实战案例)
java
爱装代码的小瓶子12 分钟前
【c++知识铺子】最后一块拼图-多态
java·开发语言·c++
认真敲代码的小火龙12 分钟前
【JAVA项目】基于JAVA的超市订单管理系统
java·开发语言·课程设计
油丶酸萝卜别吃15 分钟前
在springboot项目中怎么发送请求,设置参数,获取另外一个服务上的数据
java·spring boot·后端
7哥♡ۣۖᝰꫛꫀꪝۣℋ19 分钟前
SpringBoot 配置⽂件
java·spring boot·后端
TroubleBoy丶20 分钟前
Docker可用镜像
java·linux·jvm·docker
a37221077424 分钟前
HikariCP配置 高并发下连接泄漏避免
java·数据库·oracle