Spring Cloud之Gateway集成Knife4j
    		       		warning:
    		            这篇文章距离上次修改已过424天,其中的内容可能已经有所变动。
    		        
        		                
                
@Configuration
public class Knife4jConfiguration {
 
    @Bean
    public Docket createRestApi(Environment environment) {
        // 设置swagger的一些基本的信息
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("1.0")
                .select()
                // 对所有的API进行扫描
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                // 对所有的API进行扫描
                .paths(PathSelectors.any())
                .build();
        // 判断是否是dev环境
        if (environment.getActiveProfiles().length != 0 && "dev".equals(environment.getActiveProfiles()[0])) {
            docket.pathMapping("/");
        } else {
            docket.pathMapping("/");
        }
        return docket;
    }
 
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Cloud Gateway 接口文档")
                .description("Spring Cloud Gateway 接口文档")
                .version("1.0")
                .build();
    }
}这个代码实例定义了一个配置类Knife4jConfiguration,它使用了Docket类来配置Swagger 2.0的文档属性,并设置了API信息。它还根据当前环境(dev或非dev)来决定路径映射。这个例子展示了如何将Swagger集成到Spring Cloud Gateway中,并根据不同的环境配置不同的路径映射。
评论已关闭