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

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

相关推荐
AI_Gump14 分钟前
【AI阅读】20250717阅读输入
java·spring boot·spring
找不到、了34 分钟前
Java排序算法之<插入排序>
java·算法·排序算法
设计师小聂!43 分钟前
力扣热题100----------53最大子数组和
java·数据结构·算法·leetcode
笠码1 小时前
JVM Java虚拟机
java·开发语言·jvm·垃圾回收
thginWalker1 小时前
八股文之JVM
java
Cyanto2 小时前
MyBatis-Plus高效开发实战
java·开发语言·数据库
qhd吴飞2 小时前
mybatis 差异更新法
java·前端·mybatis
YuTaoShao2 小时前
【LeetCode 热题 100】51. N 皇后——回溯
java·算法·leetcode·职场和发展
null不是我干的2 小时前
基于黑马教程——微服务架构解析(一)
java·微服务·架构
Bonnie_12152 小时前
04-netty基础-Reactor三种模型
java·nio·jetty