我不擅长 swagger 和相关库,如果标题令人困惑,很抱歉。
我有几个服务,它们通过 swagger-ui 以 json 格式提供 api 作为 swagger 文档。接下来,我有一个 springboot 服务,它是前面提到的服务的代理,它提供所有服务的组合 api(可以在下拉列表中选择确切的服务)。它是这样实现的:
public class PropertyResourceProvider implements SwaggerResourcesProvider {
@Autowired
private SwaggerConfigProperties swaggerConfigProperties; // Just a mapping of config
@Override
public List<SwaggerResource> get() {
// Doc for local (proxy) service
val local = new SwaggerResource();
local.setName(title);
local.setUrl(swaggerLocal);
local.setSwaggerVersion(version);
// Add other services - specified in config
List<SwaggerResource> services = swaggerConfigProperties.getServices().stream().map(service -> {
String contextPath = service.get("contextPath");
String externalPath = SWAGGER_DOC_BASE_PATH + "/" + contextPath;
val resource = new SwaggerResource();
resource.setName(service.get("name"));
resource.setUrl(externalPath);
resource.setSwaggerVersion(service.get("version"));
return resource;
}).collect(Collectors.toList());
services.add(local);
return services; // Return combined API
}
现在,我们正在从 springfox 迁移到 springdoc,我无法使用 SwaggerResource 和 SwaggerResourcesProvider。我如何使用 springdoc 实现相同的功能?
在
springdoc
中,它被 AbstractSwaggerUiConfigProperties.SwaggerUrl
类取代,尽管它有点不同(例如,不再有 swaggerVersion
属性)。
public static class SwaggerUrl {
@JsonProperty("url")
private String url;
@JsonIgnore
private String name;
@JsonProperty("name")
private String displayName;
这适用于外部 API,其功能与
SwaggerResource
相同。
另外,在 application.properties
中,通过提供此属性:springdoc.swagger-ui.urls-primary-name
,您可以指定下拉列表中第一个出现的规格。
但是有一件事:请记住,在
springfox
中,您可以像这样定义安全 bean:
@Bean
public SecurityConfiguration security() {
return new SecurityConfiguration(CLIENT_ID, CLIENT_SECRET, REALM,
APP_NAME, API_KEY_VALUE,
ApiKeyVehicle.HEADER, API_TOKEN_HEADER,
SCOPE_SEPARATOR);
}
它正在向 Swagger UI 的所有传出请求添加安全上下文(在本例中为 API 标头)。但是,
springdoc
不支持此功能,因此您需要在每个API中定义安全模式,以便它们出现在您的代理服务Swagger中。
我遇到了这个问题,发现这有点烦人,不支持全局请求安全功能。