springcloud-alibaba基于nacos整合knife4j接口文档

参考 官方文档

参考 Eureka注册中心聚合OpenAPI文档

目录

一、api-doc模块添加pom依赖

二、配置yml

三、主类

四、微服务引入pom

五、配置类Knife4jConfiguration

六、接口组

七、接口与参数

八、测试


一、api-doc模块添加pom依赖


    
    
        org.springframework.boot
        spring-boot-starter-web
    
        
    
        com.github.xiaoymin
        knife4j-aggregation-spring-boot-starter
        2.0.8
    

springcloud-alibaba基于nacos整合knife4j接口文档_第1张图片

二、配置yml

server:
  port: 10909
knife4j:
  enableAggregation: true
  nacos:
    enable: true
    serviceUrl: http://localhost:8848/nacos
    routeAuth:
      enable: true
      username: nacos
      password: nacos
    routes:
      - name: 订单服务
        serviceName: service-order
        location: /v2/api-docs?group=default
        servicePath: /order/prod

springcloud-alibaba基于nacos整合knife4j接口文档_第2张图片

三、主类

package com.aipdoc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DocApplication {
public static void main(String[] args) {
        SpringApplication.run(DocApplication.class);
    }
}

springcloud-alibaba基于nacos整合knife4j接口文档_第3张图片

至此,api-doc模块已经配置好了。如果有gateway网关,要放行api路径,在官方文档中有指引,这里只是做一个简单示例。接下来是在各个微服务模块中配置knife4j。

四、微服务引入pom


        
        
            com.github.xiaoymin
            knife4j-micro-spring-boot-starter
            3.0.2
        

springcloud-alibaba基于nacos整合knife4j接口文档_第4张图片

五、配置类Knife4jConfiguration

package com.yushanma.config;

import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.Order;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@Import(BeanValidatorPluginsConfiguration.class)
public class Knife4jConfiguration {

@Bean(value = "orderApi")
@Order(value = 1)
public Docket groupRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(groupApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yushanma.controller"))
                .paths(PathSelectors.any())
                .build();
    }

private ApiInfo groupApiInfo() {
return new ApiInfoBuilder()
                .title("swagger-bootstrap-ui很棒~~~!!!")
                .description("
swagger-bootstrap-ui-demo RESTful APIs
") .termsOfServiceUrl("http://www.group.com/") .contact(getContact()) .version("1.0") .build(); } private Contact getContact(){ return new Contact("sam","none","[email protected]"); } }

springcloud-alibaba基于nacos整合knife4j接口文档_第5张图片

注意:  RequestHandlerSelectors.basePackage("com.yushanma.controller")  扫描的包名应该是 本微服务下的控制器所在包路径

六、接口组

@Api(tags = "接口组名字")

springcloud-alibaba基于nacos整合knife4j接口文档_第6张图片

七、接口与参数

// 编写示例, @ApiOperation指明是什么接口,@ApiImplicitParams指明参数是什么

@ApiOperation(value = "测试接口", notes = "studentMapper与iStudentService的测试")
@ApiImplicitParams({@ApiImplicitParam(name = "msg", required = true, paramType = "path")})

springcloud-alibaba基于nacos整合knife4j接口文档_第7张图片

八、测试

先启动其他微服务,最后启动api-doc微服务,访问文档中心 http://localhost:10909/doc.html#/home,我们就可以看到编写好的接口,以及进行在线调试接口

springcloud-alibaba基于nacos整合knife4j接口文档_第8张图片

springcloud-alibaba基于nacos整合knife4j接口文档_第9张图片

你可能感兴趣的:(禅与Java,spring,boot,spring)