需要一些帮助与springdoc ui!
我正在使用来呈现我的API模式。这是它的版本细节。
现在,我已经在我的spring引导应用程序中做了一些配置,如下所示
现在,我希望当我点击localhost:15041/swagger-ui.html
时,它会默认地带我到http://localhost:15041/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config
,并呈现我的API文档,但不幸的是它只呈现Petstore。即使我直接转到http://localhost:15041/swagger-ui.html?configUrl=/v3/api-docs/swagger-config
,它也不会使用swagger-config呈现。
虽然如果我去localhost:15041/v3/api-docs
,我得到了json文档和localhost:15041/v3/api-docs/swagger-config
,但是我得到了swagger-config。此外,如果我在petstore页面的探索搜索区域中输入/v3/api-docs
,它将显示我的API文档。
我花了超过1.5天来修复它,但它只是不起作用。真的很感激有人能帮忙。
发布于 2022-04-10 14:08:11
发布于 2022-04-02 22:12:10
从理论上讲,如果您使用默认的内容,则不需要定义任何选项,我的配置如下所示:
springdoc:
swagger-ui:
disable-swagger-default-url: true
urls[0]:
name: 'name'
url: '/v3/api-docs'
要获得完整的参考和其他有用的设置,请看这里:https://springdoc.org/#swagger-ui-properties
此外,您还提到,如果您调用http://localhost:15041/swagger-ui.html?configUrl=/v3/api-docs/swagger-config --这是错误的url --它不起作用。正确的一个是您前面提到的url:http://localhost:15041/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config。但是localhost:15041/swagger-ui.html应该正确路由。
如果使用spring-security,则还应该添加以下依赖性:springdoc security
如果它不起作用,我想一定是和你的助手有关。
发布于 2022-06-14 10:58:29
我找到了一种方法,在springdoc 1.6.9上禁用缺省URL,并使用springboot 2.6.6。
我刚刚在最新的springdoc版本1.6.9中得到了同样的问题,它总是指向默认的petstore swagger,甚至还有springdoc属性(禁用默认URL)已经配置过。多亏了Agrawal,我发现springdoc用变压器更改了这个默认URL,换句话说,它匹配包含默认SWAGGER_INITIALIZER_JS = "swagger-initializer.js"
存储URL的html.replace(Constants.SWAGGER_UI_DEFAULT_URL, StringUtils.EMPTY);
js文件的请求URL,然后将默认URL替换为html.replace(Constants.SWAGGER_UI_DEFAULT_URL, StringUtils.EMPTY);
中的空字符串。
但是在1.6.9上,这个代码段代码不能工作,因为swagger默认URL已经移动到了webjars/swagger-ui/index.html中的swagger-initializer.js文件中,并且这个js文件被删除了,这样就永远不会发生替换了。
为此,我必须使用1.6.9,因为它增加了我需要的功能,所以我修改代码,通过重写SwaggerIndexTransformer
Bean来禁用默认的URL。由于原始代码,更好地设置的配置URL无法工作。
@Bean
SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties,
SwaggerUiConfigParameters swaggerUiConfigParameters, SwaggerWelcomeCommon swaggerWelcomeCommon, ObjectMapperProvider objectMapperProvider) {
return new SwaggerIndexPageTransformer(swaggerUiConfig, swaggerUiOAuthProperties, swaggerUiConfigParameters, swaggerWelcomeCommon, objectMapperProvider){
@Override
public Resource transform(HttpServletRequest request, Resource resource,
ResourceTransformerChain transformerChain) throws IOException {
if (swaggerUiConfigParameters.getConfigUrl() == null){
System.out.println("You should set config url in case geting into this block");
// swaggerWelcomeCommon.buildFromCurrentContextPath(request);
//buildFromCurrentContextPath is not allowed to be invoked.
}
final AntPathMatcher antPathMatcher = new AntPathMatcher();
boolean isIndexFound = antPathMatcher.match("**/swagger-ui/index.html", resource.getURL().toString());
if (isIndexFound) {
String html = defaultTransformations(resource.getInputStream());
return new TransformedResource(resource, html.getBytes());
}
else
return resource;
}
};
}
将这段代码放入@Configuration类中,它可以在springdoc中覆盖Bean。并将这些属性添加到application.properties文件中:
spring.main.allow-bean-definition-overriding=true
springdoc.swagger-ui.disable-swagger-default-url=true
springdoc.swagger-ui.configUrl=/{contextpath}/v3/api-docs/swagger-config
springdoc.swagger-ui.url=/{contextpath}/v3/api-docs
https://stackoverflow.com/questions/71721477
复制相似问题