这篇文章主要介绍“springcloud gateway集成knife4j的方法是什么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“springcloud gateway集成knife4j的方法是什么”文章能帮助大家解决问题。
springcloud-gateway集成knife4j
环境信息
环境信息
spring-boot:2.6.3
spring-cloud-alibaba:2021.0.1.0
knife4j-openapi2-spring-boot-starter:4.0.0
准备工作
各微服务&网关引入依赖
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-openapi2-spring-boot-starter</artifactId> <version>4.0.0</version> </dependency>
微服务集成knife4j 第一步:编写Knife4jApiInfoProperties
import com.ideaaedi.springcloud.jd.commonspring.config.Knife4jConfig; import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * api 基础信息配置。更多配置信息项见{@link Knife4jConfig} * * @author <font size = "20" color = "#3CAA3C"><a href="https://www.maopiaopiao.com">
第二步:编写配置类Knife4jConfig
import com.ideaaedi.springcloud.jd.commonds.constant.BaseConstant; import com.ideaaedi.springcloud.jd.commonspring.config.properties.Knife4jApiInfoProperties; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.ApiKey; import springfox.documentation.service.AuthorizationScope; import springfox.documentation.service.Contact; import springfox.documentation.service.SecurityReference; import springfox.documentation.service.SecurityScheme; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spi.service.contexts.SecurityContext; import springfox.documentation.spring.web.plugins.Docket; import java.util.ArrayList; import java.util.List; /** * knife4j配置类 * * @author <font size = "20" color = "#3CAA3C"><a href="https://www.maopiaopiao.com">
第三步:放行相关静态资源
对于管控了权限的应用,应放行以下资源
# 需要放行的资源已经定义进上面编写的Knife4jConfig中 public static String[] RESOURCE_URLS = new String[]{"/webjars/**", "/swagger**", "/v2/api-docs", "/doc.html"};
网关集成knife4j
编写配置类Knife4jGatewayConfig
import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.gateway.config.GatewayProperties; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.support.NameUtils; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Mono; import springfox.documentation.swagger.web.SecurityConfiguration; import springfox.documentation.swagger.web.SecurityConfigurationBuilder; import springfox.documentation.swagger.web.SwaggerResource; import springfox.documentation.swagger.web.SwaggerResourcesProvider; import springfox.documentation.swagger.web.UiConfiguration; import springfox.documentation.swagger.web.UiConfigurationBuilder; import javax.annotation.Resource; import java.util.ArrayList; import java.util.Comparator; import java.util.LinkedHashSet; import java.util.List; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; /** * 网关knife4j配置 * * @author <font size = "20" color = "#3CAA3C"><a href="https://www.maopiaopiao.com">
测试验证
启动微服务后,访问
{网关}/doc.html完成验证