Java Spring Swagger不同的API文档

7

我有一个Spring应用程序,其中我开放了两个REST接口供使用。一个是内部开发人员使用的,另一个是为客户准备的。

Swagger生成了一个漂亮的文档,可通过/swagger-ui.html访问。
在此URL下,它显示了内部和外部用户的文档。

这是我的代码设置:

import org.springframework.boot.info.BuildProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2UiConfiguration extends WebMvcConfigurerAdapter {

    @Bean(name="restInternalSwaggerApi")
    public Docket internalApi(BuildProperties build) {
        final Docket docket = new Docket(DocumentationType.SWAGGER_2)
            .groupName( "internal" )
            .select()
                .apis( RequestHandlerSelectors.basePackage("com.xyz.web.internal") )
            .build();
        return docket;
    }

    @Bean(name="restPublicSwaggerApi")
    public Docket publicApi(BuildProperties build) {
        final Docket docket = new Docket(DocumentationType.SWAGGER_2)
            .groupName( "public" )
            .select()
                .apis( RequestHandlerSelectors.basePackage("com.xyz.web.public") )
            .build();
        return docket;
    }
}

现在,我想要将这些swagger-ui文档分离出来,以便我们的内部开发人员可以像这样访问它:
/documentation/private/swagger-ui.html
/documentation/public/api-v1.html

两者互相不可见。 如何实现?

我在这里找到了一些线索,但对我来说并不是很有用:

http://springfox.github.io/springfox/docs/current/#q13和相关资源链接
Customize endpoints of dockets with springfox Swagger
swagger multiple versions in path
https://github.com/springfox/springfox/issues/963
https://github.com/springfox/springfox/issues/1263#issuecomment-210839308

如果有人能提供正确的文档,我也会很高兴。
让我知道问题是否难以理解以及如何改进。

有关Java的Maven版本信息:

<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>

<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>

换句话说:

我希望通过不同的URL调用由下拉框访问的不同API:

enter image description here

我希望这样做,这样我就可以给客户提供一个与给我的开发同事不同的具有不同API的链接。


你找到解决方案了吗? - Dmitriy
@Dirk,你能想出如何实现它吗? - aliceangel
1个回答

0
  1. Swagger-ui是一个模块(Swagger UI解决方案中的许多jar文件之一),其中包含html和css等内容,因此如果您需要另一页(在您的情况下为api-v1.html),则需要添加另一个html文件。

  2. 这里是基于 swagger-ui源码 创建的修改后的springfox.js文件(适用于2.10.5版本),它允许仅显示一个doctet,该doctet具有groupName==页面前缀(例如,如果您有docket.groupName("external"),它将显示在页面external-swagger-ui.html上)

  3. 因此,为了使其起作用,您需要添加2个文件:文件位置 请注意,修改过的html文件也要使用自定义springfox.js < script src="springfox.js"> </script>而不是与springfox-ui jar一起提供的js文件。(在我的情况下,它是资源根目录,如屏幕截图所示)

  4. 默认的swagger-ui.html与此解决方案无关。

  5. 工作示例


嗨@vadimmityanin,请将您代码的重要部分与答案一起放置。 - rbrtl
@vadimmityanin,我在你的git仓库中看到了2个springfox.js文件。为什么需要2个js文件?我尝试了你的代码,但是它对我没有用。在swagger-ui.html中仍然可以看到2个文档组可见。 - aliceangel
@aliceangel 如果你启动我的项目(Java 8),请访问http://localhost:8080/external-swagger-ui.html,你将只能看到外部docket bean内容。 - vadimmityanin
@rbrtl 最重要的部分是我所做的 springfox.js 修改。每个 Docket 内容都可以与其页面相关联(只需复制 external-swagger-ui.html 并将其名称中的“external”单词更改为您的 docket 组)。code // 仅显示具有相应前缀的 dockets ... code 这部分来自 springfox-not-minified-source/springfox.js,它调节了该行为,但不是必需的,它只是用于参考,为了使应用程序正常工作,您需要在 META-INF/resources/springfox.js 中有一个。 - vadimmityanin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接