Swagger 导出json

打印 上一主题 下一主题

主题 1712|帖子 1712|积分 5136

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
在利用Swagger(特别是与Spring Boot集成时利用的xiaoymin.Swagger库)时,你大概希望导出Swagger的JSON配置文件,以便于在不同的情况或工具中利用,例如API测试工具Postman。以下是怎样导出Swagger JSON配置的步骤:
1. 确保Swagger配置精确

首先,确保你的项目中已经精确集成了Swagger,并且有相应的配置。例如,利用Swagger2或Swagger3的配置方式大概如下:
对于Swagger2:

在Spring Boot项目中,你可以在启动类或者配置类中添加如下配置:
  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import springfox.documentation.builders.RequestHandlerSelectors;
  4. import springfox.documentation.spi.DocumentationType;
  5. import springfox.documentation.spring.web.plugins.Docket;
  6. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  7. @Configuration
  8. @EnableSwagger2
  9. public class SwaggerConfig {                                    
  10.     @Bean
  11.     public Docket api() {
  12.         return new Docket(DocumentationType.SWAGGER_2)  
  13.           .select()                                 
  14.           .apis(RequestHandlerSelectors.basePackage("com.yourcompany.yourproject"))
  15.           .build();                                          
  16.     }
  17. }
复制代码
对于Swagger3:

对于Swagger3,配置方式略有不同:
  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import springfox.documentation.builders.RequestHandlerSelectors;
  4. import springfox.documentation.spi.DocumentationType;
  5. import springfox.documentation.spring.web.plugins.Docket;
  6. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  7. import springfox.documentation.oas.annotations.EnableOpenApi;
  8. import io.swagger.v3.oas.models.OpenAPI;
  9. import io.swagger.v3.oas.models.info.Info;
  10. @Configuration
  11. @EnableOpenApi // 注意这里使用的是 EnableOpenApi 而不是 EnableSwagger2
  12. public class SwaggerConfig {                                    
  13.     @Bean
  14.     public Docket api() {
  15.         return new Docket(DocumentationType.OAS_30)  
  16.           .select()                                 
  17.           .apis(RequestHandlerSelectors.basePackage("com.yourcompany.yourproject"))
  18.           .build();                                          
  19.     }
  20.    
  21.     @Bean
  22.     public OpenAPI openAPI() {
  23.         return new OpenAPI().info(new Info().title("Your API Title").version("1.0"));
  24.     }
  25. }
复制代码
. 导出Swagger JSON配置文件

方法1:通过Swagger UI界面导出


  • 启动你的Spring Boot应用。
  • 打开浏览器,访问 http://localhost:8080/swagger-ui/index.html(对于Swagger2)或 http://localhost:8080/swagger-ui/(对于Swagger3)。确保端标语与你的应用配置相匹配。
  • 在Swagger UI界面中,通常在右上角有一个“Export”或“Download”按钮,点击它,然后选择“Download file”,这将下载一个swagger.json文件。
方法2:通过API直接获取JSON文件

你可以直接通过访问Swagger的JSON URL来获取JSON文件:


  • 对于Swagger2: http://localhost:8080/v2/api-docs
  • 对于Swagger3: http://localhost:8080/v3/api-docs 或 http://localhost:8080/v3/api-docs/swagger-config(取决于你的配置)
    访问这个URL将直接返回JSON格式的Swagger配置。你可以利用浏览器下载此JSON,或者利用curl等工具:
    1. curl http://localhost:8080/v2/api-docs > swagger.json
    复制代码
    或者对于Swagger3:
    1. curl http://localhost:8080/v3/api-docs > swagger3.json
    复制代码
    3. 利用导出的JSON文件

    导出的JSON文件可以在Postman、API测试工具、或其他需要API定义的场景中利用。例如,在Postman中,你可以导入这个JSON文件来快速设置API哀求。在Postman中,选择“Import”然后选择或拖放你的`swagger.json

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

伤心客

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表