springboot2.6x 中使用Swagger3

前言

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

效果

在这里插入图片描述

1、相关依赖

        <!-- 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 配置文件

在这里插入图片描述

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.youkuaiyun.com/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

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

### 回答1: Spring Boot 2.6整合Swagger可以通过以下步骤实现: 1. 在pom.xml文件中添加Swagger依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3..</version> </dependency> ``` 2. 在启动类上添加@EnableSwagger2注解: ```java @SpringBootApplication @EnableSwagger2 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 3. 配置Swagger: ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API文档") .description("API文档") .version("1.") .build(); } } ``` 4. 在Controller类上添加Swagger注解: ```java @RestController @Api(tags = "用户管理") @RequestMapping("/user") public class UserController { @ApiOperation(value = "获取用户列表", notes = "获取用户列表") @GetMapping("/list") public List<User> list() { return userService.list(); } @ApiOperation(value = "添加用户", notes = "添加用户") @PostMapping("/add") public void add(@RequestBody User user) { userService.add(user); } } ``` 以上就是Spring Boot 2.6整合Swagger的基本步骤,可以根据实际需求进行配置和调整。 ### 回答2: Spring Boot是一个快速开发的开源框架,其对Spring框架进行了封装,简化了Spring应用的配置和部署。Swagger是一个RESTful风格的API框架,可以帮助我们根据API生成文档、测试API、交互式地调用API等。 在Spring Boot 2.6中,整合Swagger变得更加简单,只需要引入相应的依赖即可开始使用。具体的步骤如下: 1. 引入Swagger依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 2. 配置Swagger 在程序的启动文件中添加Swagger配置类,如下: ``` @Configuration @EnableSwagger2WebMvc public class SwaggerConfiguration { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("REST API Documentation") .description("Example API Documentation") .version("1.0.0") .build(); } } ``` 在上面的代码中,我们配置了Swagger Documentation的基本信息,并指定了要扫描的包。 3. 启动应用程序 配置完成后,我们可以启动应用程序了。当应用程序成功启动后,我们可以通过访问http://<host>:<port>/swagger-ui/来查看Swagger UI并测试API。 在Swagger UI界面中,我们可以找到每个API端点,并查看其文档和参数。我们可以测试每个API端点并查看其响应。 在整合Swagger过程中,需要注意的是,我们需要保证Swagger和Spring Boot的版本兼容,否则会引起一些异常或错误。同时,在编写API时,我们需要注意编写好文档和注释,以便Swagger可以正确生成API文档。 综上所述,Spring Boot 2.6整合Swagger非常简单,只需几个简单的步骤即可完成。Swagger使得API文档化和API测试变得更加简单,减少了我们的工作量和出错率。 ### 回答3: Spring Boot是一个非常流行的Java Web开发框架,而Swagger是一个API文档工具。通过使用Swagger,我们可以非常方便地为我们的API生成文档。在本篇文章中,我们将详细介绍如何在Spring Boot 2.6中整合Swagger。 一、添加依赖 Swagger是一个开源的项目,我们可以通过Maven或Gradle进行依赖的配置。在这里,我们以Maven为例,添加以下依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 二、添加Swagger配置 在Spring Boot 2.6中,我们可以使用@EnableSwagger2WebMvc注解来启用Swagger。在配置类上添加该注解即可: ```java @Configuration @EnableSwagger2WebMvc public class SwaggerConfig { } ``` 我们也可以通过SwaggerDocket配置对象来定制Swagger的一些属性,例如API基本信息、扫描包路径等: ```java @Configuration @EnableSwagger2WebMvc public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot Application") .description("This is a sample Spring Boot application using Swagger.") .version("1.0.0") .build(); } } ``` 在这个例子中,我们定义了API的基本信息:标题、描述和版本号。我们还使用了RequestHandlerSelectors.basePackage方法来扫描指定包路径下的Controller类。路径PathSelectors.any表示扫描所有路径。 三、访问Swagger UI 配置完成后,我们就可以通过Swagger UI来查看API文档了。在浏览器中输入http://localhost:8080/swagger-ui.html,就可以进入Swagger UI页面了。 在Swagger UI页面中,我们可以看到应用程序的所有API列表。我们可以单击每个API来查看它的详细信息,例如输入参数、输出参数等等。在右上角的控制台中,我们可以尝试发送HTTP请求,并查看响应结果。 总结 本文介绍了如何在Spring Boot 2.6中整合Swagger。我们添加了Swagger依赖,配置了SwaggerDocket对象,并在浏览器中查看了API文档。Swagger使得我们可以很容易地为我们的API生成文档,从而快速了解和测试API。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值