当前位置: 代码迷 >> 综合 >> Gateway 整合 swagger 踩坑
  详细解决方案

Gateway 整合 swagger 踩坑

热度:52   发布时间:2023-12-14 19:11:30.0

一、导入依赖

注意版本问题,这里的swagger-bootstrap-ui依赖版本使用1.8.9不要使用1.9.6,不然不会加载 /swagger-resource

		<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency><dependency><groupId>com.github.xiaoymin</groupId><artifactId>swagger-bootstrap-ui</artifactId><version>1.8.9</version></dependency>

二、Gateway网关配置

第一种、手动配置各个模块的swagger api 路径

@Component
public class SwaggerProvider implements SwaggerResourcesProvider {
    @Overridepublic List<SwaggerResource> get() {
    List<SwaggerResource> resources = new ArrayList<>();/*** /customer为application里面配置的转发,* 后面部分v2/api-docs为固定写法* 2.0表示版本 可以不传任何值*/resources.add(swaggerResource("商品系统", "/goods/v2/api-docs", ""));resources.add(swaggerResource("后台系统", "/api/v2/api-docs", ""));return resources;}private SwaggerResource swaggerResource(String name, String location, String version) {
    SwaggerResource swaggerResource = new SwaggerResource();swaggerResource.setName(name);swaggerResource.setLocation(location);swaggerResource.setSwaggerVersion(version);return swaggerResource;}
}

第二种:根据Gateway的application.yml文件的映射路径自动获取所有模块的swagger文档

@Componentpublic class MySwaggerResourceProvider implements SwaggerResourcesProvider {
    /*** swagger2默认的url后缀*/private static final String SWAGGER2URL = "/v2/api-docs";/*** 网关路由*/private final RouteLocator routeLocator;/*** 网关应用名称*/@Value("${spring.application.name}")private String self;@Autowiredpublic MySwaggerResourceProvider(RouteLocator routeLocator) {
    this.routeLocator = routeLocator;}@Overridepublic List<SwaggerResource> get() {
    List<SwaggerResource> resources = new ArrayList<>();List<String> routeHosts = new ArrayList<>();// 由于我的网关采用的是负载均衡的方式,因此我需要拿到所有应用的serviceId// 获取所有可用的host:serviceIdrouteLocator.getRoutes().filter(route -> route.getUri().getHost() != null).filter(route -> !self.equals(route.getUri().getHost())).subscribe(route -> routeHosts.add(route.getUri().getHost()));// 记录已经添加过的server,存在同一个应用注册了多个服务在nacos上Set<String> dealed = new HashSet<>();routeHosts.forEach(instance -> {
    // 拼接url,样式为/serviceId/v2/api-info,当网关调用这个接口时,会自动通过负载均衡寻找对应的主机String url = "/" + instance + SWAGGER2URL;if (!dealed.contains(url)) {
    dealed.add(url);SwaggerResource swaggerResource = new SwaggerResource();swaggerResource.setUrl(url);swaggerResource.setName(instance);resources.add(swaggerResource);}});return resources;}}

三、Gateway网关加载各个模块的swagger信息 /swagger-resource

swagger-bootstrap-ui依赖版本使用1.8.9,不然不会自动加载 /swagger-resource

@RestController
@RequestMapping("/swagger-resources")
public class SwaggerResourceController {
    private SwaggerProvider swaggerProvider;// TODO: 添加权限校验@Autowiredpublic SwaggerResourceController(SwaggerProvider swaggerProvider) {
    this.swaggerProvider = swaggerProvider;}@RequestMapping(value = "/configuration/security")public ResponseEntity<SecurityConfiguration> securityConfiguration() {
    return new ResponseEntity<>(SecurityConfigurationBuilder.builder().build(), HttpStatus.OK);}@RequestMapping(value = "/configuration/ui")public ResponseEntity<UiConfiguration> uiConfiguration() {
    return new ResponseEntity<>(UiConfigurationBuilder.builder().build(), HttpStatus.OK);}@RequestMappingpublic ResponseEntity<List<SwaggerResource>> swaggerResources() {
    return new ResponseEntity<>(swaggerProvider.get(), HttpStatus.OK);}
}

四、Gateway网关路径映射配置

一定要去前缀,要保证访问各个微服务的路径是 /v2/api-docs,不然访问不到,或者在其他微服务 controller 层配置路径映射,映射到 /v2/api-docs

    gateway:routes:- id: goods_routeuri: lb://fresh-goodspredicates:- Path=/goods/**filters:- StripPrefix=1- id: system_routeuri: lb://fresh-systempredicates:- Path=/api/**filters:- StripPrefix=1

五、模块的配置

各个模块配置类似,只举例一个

@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig {
    @Beanpublic Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.monster.fresh.goods.controller"))//扫描地址.paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {
    return new ApiInfoBuilder().title("系统").description("商品api文档").version("1.0").build();}
}

六、效果图

在这里插入图片描述

七、swagger注解

https://blog.csdn.net/xiaojin21cen/article/details/78654652

  相关解决方案