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

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

相关推荐
uppp»37 分钟前
深入理解 Java 反射机制:获取类信息与动态操作
java·开发语言
m0_748256144 小时前
SpringBoot
java·spring boot·后端
阿华的代码王国5 小时前
【从0做项目】Java搜索引擎(3)
java·搜索引擎·项目
Mr.朱鹏5 小时前
针对Feign客户端请求体参数处理问题
java·jvm·spring boot·spring·spring cloud·maven·intellij-idea
涛粒子7 小时前
Spring Bean 生命周期的执行流程
java·后端·spring
刘_sy7 小时前
使用EasyExcel和多线程实现高效数据导出
java·excel·easyexcel·批量导出excel
梦幻通灵7 小时前
IDEA通过Contince接入Deepseek
java·ide·intellij-idea
web150850966417 小时前
SQL 建表语句详解
java·数据库·sql
xiangxiongfly9157 小时前
Java 设计模式之迭代器模式
java·设计模式·迭代器模式
FLZJ_KL7 小时前
【设计模式】【行为型模式】迭代器模式(Iterator)
java·设计模式·迭代器模式