spirng boot 整合swagger3

1.加入swagger3的依赖

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

2.加入配置文件

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

import io.swagger.annotations.Api;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/**
 * @ClassName: AppSwaggerConfig
 * @Author: 86178
 * @CreateTime: 2022/3/24 9:16
 * @Description:
 */
/*@EnableSwagger2*/

@Configuration
public class AppSwagger3Config {

    @Value("${swagger2.enable:false}")
    private boolean enable = false;

    @Bean("模块")
    public Docket projectApis() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("controller类")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.regex("/*.*"))
                .build()
                .apiInfo(apiInfo())
                .enable(enable);
    }

  /*  @Bean("根据controller")
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.hopson.manager.controller"))
                .paths(PathSelectors.any())
                .build();
    }*/

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("系统平台接口文档")
                .description("本文档描述了后台系统微服务接口定义")
                .termsOfServiceUrl("http://www.baidu.com/")
                .version("1.0").build();
    }

}

3.添加开关注解@EnableOpenApi

@SpringBootApplication
@EnableOpenApi
public class EduApplication {

    public static void main(String[] args) {
        SpringApplication.run(EduApplication.class, args);
    }
    
}

4.访问使用

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个开源的Java开发框架,而Swagger是一个用于构建、发布、文档化和管理API的工具。下面详细介绍如何在Spring Boot整合Swagger。 首先,你需要在pom.xml文件中添加Swagger的依赖项。在<dependencies>标签中添加以下代码: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.10.5</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.10.5</version> </dependency> ``` 然后,你需要在Spring Boot的配置类中添加相关的注解和配置。创建一个SwaggerConfig.java文件,添加以下代码: ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("your.package.name")) .paths(PathSelectors.any()) .build(); } @Bean public UiConfiguration uiConfig() { return new UiConfiguration(null, "list", "alpha", "schema", UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L); } } ``` 在上面的代码中,你需要将"your.package.name"替换为你的应用程序的包名。这将扫描该包下的所有控制器,生成API文档。 接下来,你可以启动你的Spring Boot应用程序,并访问"http://localhost:8080/swagger-ui.html"来查看生成的API文档。你将看到所有的控制器和它们的方法以及相关的参数和注释。 如果你想修改API的文档信息,你可以使用Swagger中的注解来添加说明和标注。例如,你可以在控制器的方法上添加@ApiOperation注解来描述该方法的作用。 综上所述,将Swagger整合到Spring Boot中是很简单的。你只需要添加依赖项,配置SwaggerConfig类,然后访问Swagger UI来查看生成的API文档。同时,你可以使用Swagger注解来进一步完善API文档。希望这个教程可以帮助你理解如何在Spring Boot中使用Swagger

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值