【问题标题】:Spring boot - springdoc custom url for swagger-uiSpring boot - swagger-ui 的 springdoc 自定义 url
【发布时间】:2021-06-01 06:10:44
【问题描述】:

我需要为 Swagger-ui 使用特定的 url。我尝试使用属性“springdoc.swagger-ui.path”,但它只会重定向。

application.properties: springdoc.swagger-ui.path=/helloWorld/swagger

浏览器中的网址: http://localhost:8181/helloWorld/swagger

但是当点击回车按钮时,网址会变为以下内容:

http://localhost:8181/manager/swagger-ui/index.html?configUrl=/manager/swagger/swagger.json/swagger-config

问题是,我怎样才能使路径只有 http://localhost:8181/helloWorld/swagger/index.htmlhttp://localhost:8181/helloWorld /swagger 一旦我按下回车键(我需要“swagger-ui”这个词并且 configUrl 消失)?

我正在使用 Springdoc,甚至尝试过使用 springfox

Pom.xml

  <dependency>
         <groupId>org.springdoc</groupId>
         <artifactId>springdoc-openapi-ui</artifactId>
         <version>1.5.2</version>
    </dependency>

application.properties:

springdoc.api-docs.path=/helloWorld/swagger/swagger.json
springdoc.swagger-ui.path=/helloWorld/swagger

【问题讨论】:

标签: spring spring-boot swagger swagger-ui springdoc


【解决方案1】:

我猜它取决于答案的版本。

如果您使用的是 2.8.0 及更高版本,则可以使用以下依赖项:

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

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

并创建一个控制器,它只会重定向到主 swagger 页面,如下所示:

    @Controller
public class SwaggerController {

    @RequestMapping("/")
    public String index() {
        return "redirect:swagger-ui.html";
    }
}

但是如果您使用 2.6.1 或类似版本,您可以个性化您的 swagger 配置 java 类扩展 WebMvcConfigurerAdapter 并覆盖 addViewController,如下所示:

@Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController(MAIN_URL, SWAGGER_UI_URL).setContextRelative(true);
        registry.addRedirectViewController(MAIN_URL + "/", SWAGGER_UI_URL).setContextRelative(true);
    }

就是这样。两种方法都使用相同的依赖项,但版本不同。

【讨论】:

    猜你喜欢
    • 2021-09-28
    • 2017-02-19
    • 2020-12-18
    • 2022-06-14
    • 2022-12-06
    • 2023-01-04
    • 2016-11-09
    • 2018-01-27
    • 2020-09-09
    相关资源
    最近更新 更多