CeDo
2021-05-25 fe467f6c6be0b8b1cc47d2cdef66503fcfc59e7d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.panzhihua.zuul.config;
 
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
import java.text.MessageFormat;
import java.util.*;
 
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;
 
/**
 * @program: springcloud_k8s_panzhihuazhihuishequ
 * @description: swagger
 * @author: huang.hongfa weixin hhf9596 qq 959656820
 * @create: 2020-11-26 16:57
 **/
@Configuration
@EnableSwagger2
@Primary
public class SwaggerConfig implements SwaggerResourcesProvider {
 
    //RouteLocator可以根据zuul配置的路由列表获取服务
    private final RouteLocator routeLocator;
 
    private String[]ids=new String[]{"appletsbackstage","applets","communitybackstage", "shopbackstage","gridbackstage", "gridapp"};
 
    public SwaggerConfig(RouteLocator routeLocator) {
        this.routeLocator = routeLocator;
    }
    /**
     * Gets a result.
     *
     * @return a result
     */
    @Override
    public List<SwaggerResource> get() {
        List resources = new ArrayList();
        Arrays.asList(ids).forEach(id->{
            resources.add(swaggerResource(id, MessageFormat.format("/api/{0}/v2/api-docs", id), "1.0"));
        });
        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;
    }
 
 
}