如何为 swagger 2.8.0 做友好的基本 url

how to do friendly base url for swagger 2.8.0(如何为 swagger 2.8.0 做友好的基本 url)

本文介绍了如何为 swagger 2.8.0 做友好的基本 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改 API 文档的基本访问 URL.网址是

解决方案

UPD:Springfox 被放弃

Springfox Swagger 一直是一个有点脏的解决方案,有很多不明确和错误,但到现在(2021 Q4)它已经一年多没有更新了.

最后一根稻草是 Springfox Swagger 3.0 不再适用于 Spring Boot 2.6 的事实.x.

因此,如果您阅读本文,请考虑改用 https://springdoc.org/.

<块引用>

这是一个非常简单的转换,他们做得很好记录它.https://springdoc.org/#migrating-from-springfox.

对于使用 Springfox Swagger 3.0.0 的用户

这是更改文档的基本网址的工作配置:

springfox:文档:招摇:baseUrl:/文档开放API:v3:路径:/documentation/v3/api-docs昂首阔步:v2:路径:/documentation/v2/api-docs

I'm trying to change base access url for API documentation. The url is "http://localhost:8080/swagger-ui.html". I want to get something like "http://localhost:8080/myapi/swagger-ui.html".

I use Springfox 2.8.0 Swagger, Java 8, Spring Boot 2.0 The swagger configuration is:

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    @Bean
    public Docket api(ServletContext servletContext) {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathProvider(new RelativePathProvider(servletContext) {
                    @Override
                    public String getApplicationBasePath() {
                        return "/myapi";
                    }
                })
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(Predicates.not(PathSelectors.regex("/error")))
                .build()
                .useDefaultResponseMessages(false);
    }
}

Custom path provider had to help, but I still get access to api documentation by using url "http://localhost:8080/swagger-ui.html". If I use url "http://localhost:8080/myapi/swagger-ui.html", I get 404 error. Look at the screenshot below.

解决方案

UPD: Springfox is abandoned

Springfox Swagger had always been kinda dirty solution with a lot of unclearness and bugs, but by now (2021 Q4) it hadn't been updated for more than a year.

The final straw was the fact that Springfox Swagger 3.0 doesn't work anymore with Spring Boot 2.6.x.

So, if you reading this, please, consider switching over to https://springdoc.org/ instead.

It's a pretty straightforward conversion and they do a great job of documenting it. https://springdoc.org/#migrating-from-springfox.

For those who use Springfox Swagger 3.0.0

Here's the working configuration for changing base url for docs:

springfox:
  documentation:
    swaggerUi:
      baseUrl: /documentation
    openApi:
      v3:
        path: /documentation/v3/api-docs
    swagger:
      v2:
        path: /documentation/v2/api-docs

这篇关于如何为 swagger 2.8.0 做友好的基本 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何为 swagger 2.8.0 做友好的基本 url

基础教程推荐