实现效果:
Gateway服务可以查看多个服务的接口文档;
knife4j依赖:
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency>
自定义实现SwaggerResourcesProvider,在网关中查询路由信息,生成对应的资源信息:
import lombok.RequiredArgsConstructor; import org.springframework.cloud.gateway.config.GatewayProperties; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.support.NameUtils; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import springfox.documentation.swagger.web.SwaggerResource; import springfox.documentation.swagger.web.SwaggerResourcesProvider; import java.util.ArrayList; import java.util.List; /** * @ClassName SwaggerResourcesProviderImpl * @Description 自定义实现SwaggerResourcesProvider,在网关中查询路由信息,生成对应的资源信息 * @Author XinHai.Ma * @Date 2023/8/2 15:46 * @Version 1.0 */ @Primary // 主配置 @Component("swaggerResourcesProviderImpl") @RequiredArgsConstructor public class SwaggerResourcesProviderImpl implements SwaggerResourcesProvider { /** * swagger2默认的url后缀 */ private static final String SWAGGER2_URL = "/v2/api-docs"; /** * 路由定位器 */ private final RouteLocator routeLocator; /** * 网关配置文件 */ private final GatewayProperties gatewayProperties; @Override public List<SwaggerResource> get() { List<SwaggerResource> resources = new ArrayList<>(); List<String> routes = new ArrayList<>(); //获取网关中配置的route routeLocator.getRoutes().subscribe(route -> routes.add(route.getId())); gatewayProperties.getRoutes().stream().filter(routeDefinition -> routes.contains(routeDefinition.getId())) .forEach(routeDefinition -> routeDefinition.getPredicates().stream() .filter(predicateDefinition -> "Path".equalsIgnoreCase(predicateDefinition.getName())) .forEach(predicateDefinition -> resources.add( swaggerResource( routeDefinition.getId(), predicateDefinition .getArgs() .get(NameUtils.GENERATED_NAME_PREFIX + "0") .replace("/**", SWAGGER2_URL) ) ))); return resources; } private SwaggerResource swaggerResource(String name, String location) { SwaggerResource swaggerResource = new SwaggerResource(); swaggerResource.setName(name); swaggerResource.setLocation(location); swaggerResource.setSwaggerVersion("3.0"); return swaggerResource; } }
添加一个/swagger-resources访问接口,覆盖默认:
import com.maxinhai.mesgateway.config.SwaggerResourcesProviderImpl; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import springfox.documentation.swagger.web.SwaggerResource; import javax.annotation.Resource; import java.util.List; /** * @ClassName SwaggerResourceController * @Description 添加一个/swagger-resources访问接口,覆盖默认 * @Author XinHai.Ma * @Date 2023/8/2 15:48 * @Version 1.0 */ @RestController @RequiredArgsConstructor public class SwaggerResourceController { @Resource(name = "swaggerResourcesProviderImpl") private SwaggerResourcesProviderImpl swaggerResourceProvider; @RequestMapping(value = "/swagger-resources") public ResponseEntity<List<SwaggerResource>> swaggerResources() { return new ResponseEntity<>(swaggerResourceProvider.get(), HttpStatus.OK); } }
标签:knife4j,swaggerResource,springframework,org,import,swagger,SpringCloudAlibaba,Ga From: https://www.cnblogs.com/mxh-java/p/17601135.html