伤心客 发表于 2025-4-10 07:56:43

Swagger 导出json

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

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

在Spring Boot项目中,你可以在启动类或者配置类中添加如下配置:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {                                    
    @Bean
    public Docket api() {
      return new Docket(DocumentationType.SWAGGER_2)
          .select()                                 
          .apis(RequestHandlerSelectors.basePackage("com.yourcompany.yourproject"))
          .build();                                          
    }
} 对于Swagger3:

对于Swagger3,配置方式略有不同:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.oas.annotations.EnableOpenApi;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;

@Configuration
@EnableOpenApi // 注意这里使用的是 EnableOpenApi 而不是 EnableSwagger2
public class SwaggerConfig {                                    
    @Bean
    public Docket api() {
      return new Docket(DocumentationType.OAS_30)
          .select()                                 
          .apis(RequestHandlerSelectors.basePackage("com.yourcompany.yourproject"))
          .build();                                          
    }
   
    @Bean
    public OpenAPI openAPI() {
      return new OpenAPI().info(new Info().title("Your API Title").version("1.0"));
    }
} . 导出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等工具:
[*] curl http://localhost:8080/v2/api-docs > swagger.json 或者对于Swagger3:
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企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Swagger 导出json