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

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

相关推荐
Grey Zeng7 小时前
Java SE 25新增特性
java·jdk·jdk新特性·jdk25
雨白8 小时前
Java 线程通信基础:interrupt、wait 和 notifyAll 详解
android·java
架构师沉默13 小时前
设计多租户 SaaS 系统,如何做到数据隔离 & 资源配额?
java·后端·架构
Java中文社群14 小时前
重要:Java25正式发布(长期支持版)!
java·后端·面试
每天进步一点_JL15 小时前
JVM 类加载:双亲委派机制
java·后端
用户2986985301416 小时前
Java HTML 转 Word 完整指南
java·后端
渣哥16 小时前
原来公平锁和非公平锁差别这么大
java
渣哥16 小时前
99% 的人没搞懂:Semaphore 到底是干啥的?
java
J2K16 小时前
JDK都25了,你还没用过ZGC?那真得补补课了
java·jvm·后端
kfyty72516 小时前
不依赖第三方,不销毁重建,loveqq 框架如何原生实现动态线程池?
java·架构