解决通过网关访问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 "/";
		}

	}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涛哥是个大帅比

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值