【问题标题】:How add a custom field to the generated Swagger json from SpringFox?如何将自定义字段添加到 SpringFox 生成的 Swagger json?
【发布时间】:2018-06-21 14:30:15
【问题描述】:

我正在尝试将字段 externalDocs 添加到 Springfox 的 generated Json

"externalDocs": {
    "description": "find more info here",
    "url": "https://swagger.io/about"
},

阅读 SpringFox 文档,我了解到我需要创建一个 plugin 来扩展 SpringFox 功能并添加此字段。我试过了:

@Component
@Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER + 1002)
@Slf4j
public class ExternalDocSwaggerConfiguration implements ApiListingBuilderPlugin {

    @Override
    public void apply(final ApiListingContext apiListingContext) {

        ObjectVendorExtension ext = new ObjectVendorExtension("externalDocs");
        ext.addProperty(new StringVendorExtension("description", "Link externo"));
        ext.addProperty(new StringVendorExtension("url", "https://swagger.io/about"));
        apiListingContext.apiListingBuilder().extensions(
            Collections.singletonList(ext)); // extensions does not exist
    }

    @Override
    public boolean supports(final DocumentationType documentationType) {
        return true;
    }
}

我希望添加扩展程序,如 hereOperationBuilderPlugin 所示,但 apiListingBuilder 上没有 extensions 方法。

那么,如何使用 SpringFox 在生成的 Swagger Json 的根目录上添加这个标签?

【问题讨论】:

    标签: swagger springfox


    【解决方案1】:

    2.7.0版本新增this feature

    要添加此字段externalDocs,您可以使用Docket 中的extensions 方法:

    @Bean
    public Docket customImplementation() {
    
        ObjectVendorExtension ext = new ObjectVendorExtension("externalDocs");
        ext.addProperty(new StringVendorExtension("description", "Link externo"));
        ext.addProperty(new StringVendorExtension("url", "https://swagger.io/about"));
    
        return new Docket(DocumentationType.SWAGGER_2)
                .extensions(Collections.singletonList(ext))
                .apiInfo(apiInfo())
                .securitySchemes(newArrayList(apiKey()))
                .pathMapping("/api")
                .securityContexts(newArrayList(securityContext())).select()
                .apis(getPackages())
                .paths(PathSelectors.any())
                .build();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多