how to do friendly base url for swagger 2.8.0(如何为 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-docsI'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
				
        
 
            
        - java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
 - 未找到/usr/local/lib 中的库 2022-01-01
 - 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
 - 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
 - 如何指定 CORS 的响应标头? 2022-01-01
 - 转换 ldap 日期 2022-01-01
 - 获取数字的最后一位 2022-01-01
 - GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
 - 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
 - Eclipse 的最佳 XML 编辑器 2022-01-01
 
						
						
						
						
						
				
				
				
				