Swagger UI 在将 validatorUrl 用作 null 后仍然显示错误
Swagger UI displays Error still after using validatorUrl as null
尽管如果我要描述的话,场景类似于
Swagger UI Displays but I get an "ERROR" indicator。一切对我来说都很好,我的路线被正确列出,但显示错误,并且在所有此类类似线程上建议的解决方案与设置 validationUrl=null
有关。
我正在使用以下依赖项 -
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>io.federecio</groupId>
<artifactId>dropwizard-swagger</artifactId>
<version>0.7.0</version>
</dependency>
这是我的注册码的样子 -
@Override
public void initialize(Bootstrap<CustomServiceConfig> bootstrap) {
bootstrap.addBundle(new SwaggerBundle<CustomServiceConfig>() {
@Override
protected SwaggerBundleConfiguration getSwaggerBundleConfiguration(
CustomServiceConfig customServiceConfig) {
return customServiceConfig.getSwaggerBundleConfiguration();
}
});
}
config.yml
中的这些配置是 -
swagger:
title: Custom Service
description: APIs for Test
contact: me@alias.com
uriPrefix: /
resourcePackage: com.x.y.package
我已经尝试更新上面 .yml
中的 SwaggerBundleConfiguration 以添加
validatorUrl=null
但这似乎也不能解决问题。如何解决?
必须将 validatorURL: null
插入到 swagger ui 的构造函数中 在 swagger index.html
文件 中,而不是 Dropwizard yaml 配置文件 [see the SO post you referenced]。
所以你有几个选择,但首先我不推荐 dropwizard-swagger
包,因为它似乎已经远远落后于 Dropwizard 本身。根据 version documentation on its page,它最多只支持 Dropwizard 0.8 和 swagger-ui 2.4。 Dropwizard 在 1.x 和 swagger-ui 3.x。
选项 1
如果您取消 dropwizard-swagger
库,您可以将 swagger-ui 资产直接放入资源目录下的项目中,并向您的 Dropwizard 应用程序添加一个 AssetBundle
。 This bundle allows serving of static content on your classpath. The static html you'll need is all in the dist
folder here(即将其复制到您的 dropwizard 项目中的 /resources)。
你会有这样的东西
@Override
public void initialize(Bootstrap<CustomServiceConfig> bootstrap) {
bootstrap.addBundle(new AssetsBundle("/assets/swagger", "/swagger", "index.html"));
}
它将像 dropwizard-swagger
lib 一样在 localhost:<port>/swagger
上为 swagger UI 服务。
要使此解决方案生效,您还需要生成 swagger json 文件并将其放入 build 目录(也是资源路径)中的正确路径中。 The swagger-maven-plugin
can help do this for you. Here is a link to some discussion around how to bundle the static swagger ui and swagger json in your app. You'll need to modify the index.html
to point at your swagger json file. The line of code is here.
const ui = SwaggerUIBundle({
url: "/path/relative/to/your/java/jar/swagger-api.json",
validatorUrl: null,
...
}
选项 2
您可以分叉 dropwizard-swagger
代码,修改包含的 index.html
文件以设置 validatorUrl: null
。您需要添加它的地方是 SwaggerUI
js 对象的构造函数中。你可以 see it here.
简而言之:
window.swaggerUi = new SwaggerUi({
url: url,
validatorUrl: null,
...
}
然后您需要重新构建uild 库并将这个dropwizard-swagger
库的修改版本包含在您的项目中。请注意 dropwizard-swagger
项目 just happens to create an AssetBundle
for you.
总结
无论哪种方式,您都需要修改属于 swagger ui 分发的 index.html 文件。最简单的方法可能是暂时忽略错误图标。
尽管如果我要描述的话,场景类似于
Swagger UI Displays but I get an "ERROR" indicator。一切对我来说都很好,我的路线被正确列出,但显示错误,并且在所有此类类似线程上建议的解决方案与设置 validationUrl=null
有关。
我正在使用以下依赖项 -
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>io.federecio</groupId>
<artifactId>dropwizard-swagger</artifactId>
<version>0.7.0</version>
</dependency>
这是我的注册码的样子 -
@Override
public void initialize(Bootstrap<CustomServiceConfig> bootstrap) {
bootstrap.addBundle(new SwaggerBundle<CustomServiceConfig>() {
@Override
protected SwaggerBundleConfiguration getSwaggerBundleConfiguration(
CustomServiceConfig customServiceConfig) {
return customServiceConfig.getSwaggerBundleConfiguration();
}
});
}
config.yml
中的这些配置是 -
swagger:
title: Custom Service
description: APIs for Test
contact: me@alias.com
uriPrefix: /
resourcePackage: com.x.y.package
我已经尝试更新上面 .yml
中的 SwaggerBundleConfiguration 以添加
validatorUrl=null
但这似乎也不能解决问题。如何解决?
必须将 validatorURL: null
插入到 swagger ui 的构造函数中 在 swagger index.html
文件 中,而不是 Dropwizard yaml 配置文件 [see the SO post you referenced]。
所以你有几个选择,但首先我不推荐 dropwizard-swagger
包,因为它似乎已经远远落后于 Dropwizard 本身。根据 version documentation on its page,它最多只支持 Dropwizard 0.8 和 swagger-ui 2.4。 Dropwizard 在 1.x 和 swagger-ui 3.x。
选项 1
如果您取消 dropwizard-swagger
库,您可以将 swagger-ui 资产直接放入资源目录下的项目中,并向您的 Dropwizard 应用程序添加一个 AssetBundle
。 This bundle allows serving of static content on your classpath. The static html you'll need is all in the dist
folder here(即将其复制到您的 dropwizard 项目中的 /resources)。
你会有这样的东西
@Override
public void initialize(Bootstrap<CustomServiceConfig> bootstrap) {
bootstrap.addBundle(new AssetsBundle("/assets/swagger", "/swagger", "index.html"));
}
它将像 dropwizard-swagger
lib 一样在 localhost:<port>/swagger
上为 swagger UI 服务。
要使此解决方案生效,您还需要生成 swagger json 文件并将其放入 build 目录(也是资源路径)中的正确路径中。 The swagger-maven-plugin
can help do this for you. Here is a link to some discussion around how to bundle the static swagger ui and swagger json in your app. You'll need to modify the index.html
to point at your swagger json file. The line of code is here.
const ui = SwaggerUIBundle({
url: "/path/relative/to/your/java/jar/swagger-api.json",
validatorUrl: null,
...
}
选项 2
您可以分叉 dropwizard-swagger
代码,修改包含的 index.html
文件以设置 validatorUrl: null
。您需要添加它的地方是 SwaggerUI
js 对象的构造函数中。你可以 see it here.
简而言之:
window.swaggerUi = new SwaggerUi({
url: url,
validatorUrl: null,
...
}
然后您需要重新构建uild 库并将这个dropwizard-swagger
库的修改版本包含在您的项目中。请注意 dropwizard-swagger
项目 just happens to create an AssetBundle
for you.
总结
无论哪种方式,您都需要修改属于 swagger ui 分发的 index.html 文件。最简单的方法可能是暂时忽略错误图标。