相比swagger,knife4j的ui更加直观,knife4j封装了swagger,且knife4j对于文档的分割性也更好,能够整合并独立的展示每个模块的文档,操作更加方便。
配置 pom依赖核心配置com.github.xiaoymin knife4j-spring-boot-starter 3.0.3
Knife4jConfiguration
@Configuration
@EnableSwagger2
@EnableKnife4j
public class Knife4jConfiguration {
@Bean(value = "defaultApi2")
public Docket defaultApi2() {
Docket docket=new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//这里指定Controller扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.sfcx.modules"))
.paths(PathSelectors.any())
.build();
return docket;
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// //大标题
.title("后台服务API接口文档")
// 版本号
.version("1.0.0")
// 描述
.description("后台API接口")
// 作者
.contact(new Contact("zwj","lz","xx@163.com"))
.build();
}
}
访问地址:http://ip:port /path /doc.html
聚合文档在上边配置的基础上,在增加下方配置:
当我们使用微服务或者分布式开发时,需要将所有模块的文档聚合在一起,并且设置不同的访问密码,我们就需要用到聚合文档。
在网关模块下增加配置:GatewayRoutersConfiguration、SwaggerProvider
@Slf4j
@Configuration
public class GatewayRoutersConfiguration {
@Bean
public RouterFunction indexRouter(@Value("classpath:/META-INF/resources/doc.html") final Resource indexHtml) {
return route(GET("/"), request -> ok().contentType(MediaType.TEXT_HTML).bodyValue(indexHtml));
}
}
@Component
@Primary
@AllArgsConstructor
public class SwaggerProvider implements SwaggerResourcesProvider {
public static final String API_URI = "v2/api-docs";
private final RouteLocator routeLocator;
private final GatewayProperties gatewayProperties;
@Override
public List get() {
List resources = new ArrayList<>();
List routes = new ArrayList<>();
routeLocator.getRoutes().subscribe(route ->{
String id = route.getId();
routes.add(id);
} );
gatewayProperties.getRoutes().stream().filter(routeDefinition -> routes.contains(routeDefinition.getId()))
.forEach(routeDefinition -> routeDefinition.getPredicates().stream()
.filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName()))
.forEach(predicateDefinition -> {
//knife4j不能和Spring Cloud Gateway混合使用(因为Gateway底层是基于Netty构建的),
//所以无法通过网关路由转发,只能通过服务名称直接调用
//这里默认会去拿网关路由配置的第一个配置,例如我配的/product/**,于是就不能访问,所以这里换成id拼接,id就是服务名称
String id = routeDefinition.getId();
String location = id + "/" + API_URI;
resources.add(swaggerResource(id, location));
}
));
return resources;
}
private SwaggerResource swaggerResource(String name, String location) {
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(name);
swaggerResource.setLocation(location);
swaggerResource.setSwaggerVersion("2.0");
return swaggerResource;
}
}



