Swagger集成springboot页面请求接口context-path重复问题记录_sawgger context-path-CSDN博客

目录

官方文档参考:springfox.

问题复现

springboot:2.3.3.RELEASE
整合swagger:2.10.5
在这里插入图片描述

解决方案

在网上看到的解决方案是注册了一个自定义的PathProvider

@Value("${server.servlet.context-path}")
private String servletContextPath;
/**
 * 重写 PathProvider ,解决 context-path 重复问题
 * @return
 */
@Bean
public PathProvider pathProvider() {
    return new DefaultPathProvider() {
        @Override
        public String getOperationPath(String operationPath) {
            operationPath = operationPath.replaceFirst(servletContextPath, "/");
            UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromPath("/");
            return Paths.removeAdjacentForwardSlashes(uriComponentsBuilder.path(operationPath).build().toString());
        }

        @Override
        public String getResourceListingPath(String groupName, String apiDeclaration) {
            apiDeclaration = super.getResourceListingPath(groupName, apiDeclaration);
            return apiDeclaration;
        }
    };
}

但是启动容器会有BEAN重复的问题

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'pathProvider', defined in class path resource [com/***/Knife4jConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [springfox/documentation/spring/web/SpringfoxWebConfiguration.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

Disconnected from the target VM, address: '127.0.0.1:54117', transport: 'socket'

Process finished with exit code 1

这里需要开启Springboot BEAN覆盖的功能,然后该配置的开启担心引起容器内的其他问题

spring:
  main:
    #解决SWAGGER 请求路径context-path重复问题时候BEAN重复注入问题
    allow-bean-definition-overriding: true

看了一下此方式解决问题的思路,其实是用自定义的BEAN覆盖官方提供的
在这里插入图片描述
本来是想去github提issue反馈这个路径重复的问题,后来在过程中发现有可用的最高版本也被官方建议使用
在这里插入图片描述

更优雅的解决方案-升级Swagger3.0.0

maven依赖更加简洁

<dependency>
   <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

SwaggerConfig 配置类

@Configuration
@EnableOpenApi//来自官方文档已经 Remove any @EnableSwagger2... annotations
public class SwaggerConfig {
    @Bean
    public Docket defaultApi2() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.daie.springboot.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("***")
                .description("***")
                .version("1.0.0-SNAPSHOT")
                .build();
    }
}

重写mvc中添加对swagger静态资源处理

@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {
   
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
                .resourceChain(false);
    }
}

context-path重复问题得到解决
在这里插入图片描述


-

1.Swagger升级之后配置变化
http://localhost:8080/daie/swagger-ui.html变为http://localhost:8080/daie/swagger-ui/index.html

2.WebMvcConfigurationSupport中ResourceHandlers配置的变化(展示旧版本,新版本参考上文)

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    String[] location = "classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,/".split(",");
    registry.addResourceHandler("/**")
            .addResourceLocations(location);
    registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/");
    super.addResourceHandlers(registry);
}

3.开启Swagger注解的变化

@EnableSwagger2WebMvc //2.10.5版本下的注释 在3.0.0中不建议使用
@EnableOpenApi// Remove any @EnableSwagger2... annotations(来自官方文档)

4.maven依赖更加简洁(展示旧版本,新版本参考上文)

<dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger-ui</artifactId>
     <version>2.10.5</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.10.5</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-spring-webmvc</artifactId>
    <version>2.10.5</version>
</dependency>

其他问题

问题1.浏览器出现提示,无法访问Swagger
Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:
解决1: 参考. 添加springfox-spring-webmvc依赖

问题2:访问Swagger页面控制台出现java.lang.NumberFormatException: For input string: “”
解决2:参考. 修改swagger-models版本


原网址: 访问
创建于: 2024-12-12 11:10:45
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论