| | |
| | | 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.swagger.web.SwaggerResourcesProvider; |
| | | import springfox.documentation.swagger2.annotations.EnableSwagger2; |
| | | |
| | | import java.text.MessageFormat; |
| | | import java.util.*; |
| | | |
| | | import static java.util.stream.Collectors.collectingAndThen; |
| | |
| | | @Override |
| | | public List<SwaggerResource> get() { |
| | | List resources = new ArrayList(); |
| | | List<Route> routes = routeLocator.getRoutes(); |
| | | routes = routes.stream().collect( |
| | | collectingAndThen( |
| | | toCollection(() -> new TreeSet<>(Comparator.comparing(Route::getId))), ArrayList::new) |
| | | ); |
| | | //通过RouteLocator获取路由配置,遍历获取所配置服务的接口文档,这样不需要手动添加,实现动态获取 |
| | | List<String>stringList= Arrays.asList(ids); |
| | | for (Route route: routes) { |
| | | String id = route.getId(); |
| | | boolean contains = stringList.contains(id); |
| | | String fullPath = route.getFullPath(); |
| | | String s = fullPath.replaceFirst("/api", ""); |
| | | String s1 = s.replaceFirst("/" + id, ""); |
| | | if (contains) { |
| | | resources.add(swaggerResource(id,s1.replace("**", "v2/api-docs"),"1.0")); |
| | | } |
| | | } |
| | | Arrays.asList(ids).forEach(id->{ |
| | | resources.add(swaggerResource(id, MessageFormat.format("/api/{0}/v2/api-docs", id), "1.0")); |
| | | }); |
| | | return resources; |
| | | } |
| | | |