解决通过网关访问Swagger后url路径不正确的问题

项目场景:

Spring Boot集成Swagger


问题描述

本地访问swagger可以正常访问接口url正确,上线后通过网关访问swagger,发现接口的url路径不对。

正常访问,打开swagger地址:http://localhost:8080/test/swagger-ui.html,在swagger上面访问接口:http://localhost:8080/test/interface

通过网关访问,打开swagger地址:http://localhost:8080/gateway/test/swagger-ui.html,在swagger上面访问接口:http://localhost:8080/test/interface

发现问题,接口并没有带上/gateway关键字

原因分析:

打开swagger会发现,basePath的地址是test,通过网关访问也是test,所以要变成/gateway/test才正确


解决方案:

改变basePath的值

1.properties配置文件

# spring boot请求path
server.servlet.context-path = test
# 是否走网关访问swagger,本地调试不走网关,默认false
swagger.isGat = true
# swagger版本
swagger.version=0.0.1
# 是否开启swagger
swagger.enabled = true

2.Swagger配置

package com.test.config;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.paths.AbstractPathProvider;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@ConditionalOnProperty(prefix = "swagger", name = "enabled")	//不是false就会加载bean
public class SwaggerConfig {

	//是否走网关访问swagger,本地调试不走网关,默认false
	@Value("${swagger.isGat:false}")
	private Boolean isGat;
	//修改为自己的context-path
	@Value("${server.servlet.context-path}")
	private String contextPath;

	@Value("${swagger.version}")
	private String version;
	
	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				//防止走网关swagger访问404,不走网关注释掉
				.pathProvider(new MyPathProvider())
				.genericModelSubstitutes(DeferredResult.class)
				.useDefaultResponseMessages(false)
				.forCodeGeneration(true)
				.pathMapping("/")
				.select() // 选择哪些路径和API会生成document
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) // 根据URL过滤接口
				.paths(PathSelectors.any()) // 对所有路径进行监控    
				.build()
				.apiInfo(getApiInfo());
	}

	private ApiInfo getApiInfo() {
		return new ApiInfoBuilder()
				.title("测试接口文档")
				.description("测试接口文档")
				.version(version)
				.build();
	}

	class MyPathProvider extends AbstractPathProvider {

		@Override
		protected String applicationPath() {
			String pathMapping = "";
			if(isGat){
				pathMapping = "/gateway";
			}
			return pathMapping + contextPath;
		}

		@Override
		protected String getDocumentationPath() {
			return "/";
		}

	}
} 

关键代码:

//防止走网关swagger访问404,不走网关注释掉
.pathProvider(new MyPathProvider())


class MyPathProvider extends AbstractPathProvider {

		@Override
		protected String applicationPath() {
			String pathMapping = "";
			if(isGat){
				pathMapping = "/gateway";
			}
			return pathMapping + contextPath;
		}

		@Override
		protected String getDocumentationPath() {
			return "/";
		}

	}

猜你喜欢

转载自blog.csdn.net/u011974797/article/details/130974659
今日推荐