首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Springdoc Swagger UI不使用swagger-config

Springdoc Swagger UI不使用swagger-config
EN

Stack Overflow用户
提问于 2022-04-02 22:00:47
回答 4查看 6.9K关注 0票数 4

需要一些帮助与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天来修复它,但它只是不起作用。真的很感激有人能帮忙。

EN

回答 4

Stack Overflow用户

发布于 2022-04-10 14:08:11

发现了问题。问题在于springdoc库如何使用swagger。他们编写了一些自定义代码来修补swagger-ui动态返回的index.html文件,以便在响应中注入configUrl查询参数。

现在,当swagger从4.8.1移到4.9.0时,他们使用变化 index.html文件格式,打破了用springdoc编写的自定义代码。

为了解决这个问题,目前我们需要将org.webjars:swagger-ui降级到4.8.1,其中index.html文件的预期值为springdoc,后者将遵守在查询参数中发送的configUrl。

票数 1
EN

Stack Overflow用户

发布于 2022-04-02 22:12:10

从理论上讲,如果您使用默认的内容,则不需要定义任何选项,我的配置如下所示:

代码语言:javascript
运行
复制
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

如果它不起作用,我想一定是和你的助手有关。

票数 0
EN

Stack Overflow用户

发布于 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无法工作。

代码语言:javascript
运行
复制
    @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文件中:

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71721477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档