SpringBoot整合Swagger3.0

目录

第一步:引入依赖

第二步:yml配置文件添加一下内容

第三步:添加SwaggerConfig配置类

第四步:启动类添加注解

第五步:Controller层类添加注解

 第六步:实体类添加注解

 第七步:启动项目访问Swagger


我的项目使用gradle管理依赖的

第一步:引入依赖

implementation 'io.springfox:springfox-boot-starter:3.0.0'

 maven引入依赖


        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

第二步:yml配置文件添加一下内容

spring:
 mvc:
  pathmatch:
    matching-strategy: ant_path_matcher

第三步:添加SwaggerConfig配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @author song
 */
@Configuration
@EnableOpenApi
public class SwaggerConfig {

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.OAS_30)
                /*配置Swagger信息*/
                .apiInfo(apiInfo())
                /*是否启动Swagger false浏览器中无法访问Swagger*/
                .enable(true)
                .select()
                /* apis():指定扫描的接口
                 *  RequestHandlerSelectors:配置要扫描接口的方式
                 *       basePackage:指定要扫描的包
                 *       any:扫面全部
                 *       none:不扫描
                 *       withClassAnnotation:扫描类上的注解(参数是类上注解的class对象)
                 *       withMethodAnnotation:扫描方法上的注解(参数是方法上的注解的class对象)
                 */
                .apis(RequestHandlerSelectors.basePackage("com.boyin.css.css.web.controller"))
                /**
                 * paths():过滤路径
                 *  PathSelectors:配置过滤的路径
                 *      any:过滤全部路径
                 *      none:不过滤路径
                 *      ant:过滤指定路径:按照按照Spring的AntPathMatcher提供的match方法进行匹配
                 *      regex:过滤指定路径:按照String的matches方法进行匹配
                 */
                .paths(PathSelectors.any())
                .build();
    }


    /**
     * 
     * @return
     */
    @Bean
    public ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("中国通信工业协会网格化分会接口文档")
                .description("协会接口文档")
                .contact(new Contact("协会","http://www.cciagb.com","song"))
                .version("1.0.0")
                .build();
    }

}

第四步:启动类添加注解

@EnableDiscoveryClient

第五步:Controller层类添加注解

 第六步:实体类添加注解

 第七步:启动项目访问Swagger

http://localhost:8080/swagger-ui/index.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
你好!要将Spring Boot 2.6与Swagger 3.0整合在一起,你可以按照以下步骤进行操作: 步骤1:添加Swagger依赖 在你的Spring Boot项目的pom.xml文件中,添加Swagger依赖: ```xml <dependencies> <!-- 其他依赖 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> </dependencies> ``` 步骤2:配置Swagger 创建一个Swagger配置类,用于配置Swagger的相关信息: ```java import org.springframework.context.annotation.Configuration; import springfox.documentation.swagger2.annotations.EnableSwagger2; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; @Configuration @EnableSwagger2 public class SwaggerConfig { public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API 文档") .description("API 文档") .version("1.0.0") .build(); } } ``` 步骤3:启用Swagger 在你的Spring Boot应用程序的主类上使用`@EnableSwagger2`注解来启用Swagger: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Import; @SpringBootApplication @Import(SwaggerConfig.class) public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 步骤4:访问Swagger UI 在启动你的应用程序后,你可以通过访问`http://localhost:8080/swagger-ui/`来查看生成的API文档。 以上就是将Spring Boot 2.6与Swagger 3.0整合的基本步骤。你可以根据自己的需要进一步定制和配置Swagger。希望能对你有所帮助!如果有任何其他问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

没有腰的嘟嘟嘟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值