SpringBoot2.7.*配置Swagger

Swagger的配置

提示:依赖+配置即可


1、依赖


<dependency>
     <groupId>com.github.xiaoymin</groupId>
     <artifactId>knife4j-spring-ui</artifactId>
     <version>3.0.3</version>
</dependency>

<dependency>
     <groupId>io.springfox</groupId>
     <!--提供了使用Swagger和OpenAPI规范创建API文档的支持。-->
     <artifactId>springfox-boot-starter</artifactId>
     <version>3.0.0</version>
</dependency>



2、Swagger 配置类的书写

我的控制类的路径

在这里插入图片描述


注意事项有注释

package com.yyl.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;


/**
 * swagger接口文档的配置
 */
@Configuration
@EnableSwagger2
public class Knife4jConfiguration implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         *  /**表示所有的请求路径都将被映射到指定的资源路径中。
         */
        registry.addResourceHandler("/**")
        		.addResourceLocations("classpath:/META-INF/resources/");  // 自己的配置
        registry.addResourceHandler("/doc.html")  // 配置访问路径
                .addResourceLocations("classpath:/META-INF/resources/");  // 添加Swagger的页面资源
    }

    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                // 分组名称
                .groupName("模块化开发的系统接口测试文档")
                .select()
                //这里标注控制器的位置
                .apis(RequestHandlerSelectors.basePackage("com.yyl.web.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * api信息
     * @return api对象信息
     */
    private ApiInfo apiInfo()   {
        return new ApiInfoBuilder()
                .title("模块化开发的系统接口测试文档")  // 标题
                .description("用户中心接口文档")  // 简介
                .termsOfServiceUrl("http://localhost:9090/") // Swagger API文档中的一个可选配置项,用于指定API服务的服务条款URL。
                .contact(new Contact("yyl","http://localhost:9090/","778470787@qq.com"))
                .version("1.0")
                .build();
    }
}


3、注解 @EnableWebMvc

因为Swagger配置类有实现 WebMvcConfigurer 接口,
所以在启动类上加上注解 @EnableWebMvc
在这里插入图片描述


4、控制层改造

类:

在这里插入图片描述

方法:

在这里插入图片描述


5、启动项目

访问 http://localhost:9090/doc.html 即可,doc.html路径在配置类有配置,可以更换。

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Swagger 是一个接口文档生成工具,可以方便地生成 RESTful API 文档。在 Spring Boot 中,使用 Swagger 也非常简单,只需要添加对应的依赖,然后在配置文件中进行简单的配置即可。 下面是在 Spring Boot 中添加 Swagger 的步骤: 1. 在 pom.xml 文件中添加 Swagger 的依赖: ``` <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> ``` 2. 在 Spring Boot 的启动类上添加 `@EnableSwagger2` 注解,启用 Swagger: ``` @SpringBootApplication @EnableSwagger2 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 3. 添加 Swagger 配置类,配置 Swagger 的基本信息: ``` @Configuration @EnableSwagger2 public class SwaggerConfig { @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("API 文档") .description("API 接口文档") .version("1.0.0") .build(); } } ``` 其中,`@Bean` 注解的 `Docket` 对象是 Swagger 的主要配置对象,可以设置 API 的基本信息,如文档标题、版本号等。`apis` 方法和 `paths` 方法可以设置 API 的扫描范围,这里的示例是扫描 `com.example.demo` 包下的所有 API。 4. 启动应用程序,在浏览器中访问 `http://localhost:8080/swagger-ui.html`,即可看到自动生成的 API 文档。 以上就是在 Spring Boot 中使用 Swagger 的简单步骤,你还可以根据自己的需求进行更加详细的配置

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ItHeiMa小飞机

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值