1、SpringBoot快速整合Swagger3.0

1、SpringBoot快速整合Swagger3.0

2、微服务整合Swagger3.0 - 抽取为公共模块

3、微服务整合Swagger3.0 - 网关Gateway聚合接口

4、微服务整合Swagger3.0 - 使用方法

5、微服务Swagger3.0升级为Knife4j

一、什么是Swagger

Swagger 是一款restful接口的文档在线自动生成+功能测试功能软件;

Swagger的目标是为rest apis 定义一个标准的,与语言无关的接口,使人和计算机在看不到源码或者看不到文档或者不能通过网络流量检测的情况下能发现和理解各种服务的功能。

二、springfox-swagger和springfox-swagger-ui

Swagger 是一种规范。

springfox-swagger 是基于 Spring 生态系统的该规范的实现。

springfox-swagger-ui 是对 swagger-ui 的封装,使其可以使用 Spring 的服务。

三、SpringBoot使用Swagger

1、pom文件引入依赖,版本2和3自行选择,这里的${swagger.fox.version}选了3.0.0

<!-- Swagger -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>${swagger.fox.version}</version>
</dependency>
<!-- Swagger UI -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>${swagger.fox.version}</version>
</dependency>

2、配置swagger

/**
 * @author cch
 * @create 2024-01-26 18:47
 */
@Configuration
@EnableSwagger2  //开启Swagger2的自动配置
@Import({SwaggerWebConfiguration.class}) //3.0.0版本访问是: /swagger-ui/index.html
public class SwaggerConfig{
    @Bean
    public Docket buildDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 要扫描的API(Controller)基础包,注意basePackage改成自己每个项目的路径
                .apis(RequestHandlerSelectors.basePackage("com.cch"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        Contact contact = new Contact("cch","","");
        return new ApiInfoBuilder()
                //标题
                .title("管理API文档")
                //接口描述
                .description("后台api")
                .contact(contact)
                .version("3.0.0").build();
    }
}

Swagger2访问地址:http://{ip}:{port}/swagger-ui.html

Swagger3访问地址:http://{ip}:{port}/swagger-ui/index.html

3、配置资源映射

public class SwaggerWebConfiguration implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        /** swagger-ui 地址 */
        //3.0.0版本访问是: /swagger-ui/index.html
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
    }
}

4、在Application启动类加入注解@EnableSwagger2

@EnableSwagger2
@SpringBootApplication
public class FilesApplication {
    public static void main(String[] args) {
        SpringApplication.run(FilesApplication.class, args);
    }
}

5、测试swagger

    @ApiOperation("测试Swagger")
    @GetMapping("/testSwagger")
    public Object info(){
        return "{\"username\":\"admin\",\"password\":\"admin123\"}";
    }

测试效果:

访问地址:http://{ip}:{port}/swagger-ui/index.html

注:如果不能访问,启动时报错:

Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException

请在application.properties中加入以下这段信息:

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

你好!要将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。希望能对你有所帮助!如果有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值