Swagger2 接口多级分组方法

swagger 无疑是Java开发的最佳伴侣,接口非常方便调试;当然也有用Postman,因人而异吧。
但是我们在配置Swagger的时候会有一个都默认一级分类也就是那个Defaul,在这个组里所有的接口二级分类都在这里,一般小项目还可以使用,但是做大项目的时候就接个接口就不那么方便了。
在这里插入图片描述

在这里人放一下我的Swagger 配置代码

@Bean
    public Docket buildDocket() {
    
    
        return  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())//调用下面apiInfo()方法
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.cm.aps"))//注意这里的路径,新手容易在这里出错导致打不开。
                .paths(PathSelectors.any())
                .build();
    }
    public ApiInfo apiInfo() {
    
    
        return  new ApiInfoBuilder()
                .title("模具排程相关 API")
                .description("部分数据来源:模德宝、快表数据")
                .termsOfServiceUrl("")//这里可以是项目地址
                .version("2.0.1")
                .build();
    }

    public ApiInfo apiInfoprdt() {
    
    
        return  new ApiInfoBuilder()
                .title("模具与产品 API")
                .description("基础数据配置")
                .termsOfServiceUrl("")//这里可以是项目地址
                .version("2.0.1")
                .build();
    }

那么我的目的是想把对应二级分组归到一组分类中,这样管理起来比较方便;
思路通过接口路径进行识别分组;
Swagger配置如下:

package com.cm.aps.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    
    
    @Bean
    public Docket buildDocket() {
    
    
        return  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())//调用下面apiInfo()方法
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.cm.aps"))//注意这里的路径,新手容易在这里出错导致打不开。
                .paths(PathSelectors.any())
                .build();
    }
    public ApiInfo apiInfo() {
    
    
        return  new ApiInfoBuilder()
                .title("模具排程相关 API")
                .description("部分数据来源:模德宝、快表数据")
                .termsOfServiceUrl("")//这里可以是项目地址
                .version("2.0.1")
                .build();
    }

    public ApiInfo apiInfoprdt() {
    
    
        return  new ApiInfoBuilder()
                .title("模具与产品 API")
                .description("基础数据配置")
                .termsOfServiceUrl("")//这里可以是项目地址
                .version("2.0.1")
                .build();
    }

    @Bean
    public Docket web_api_prdt() {
    
    
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfoprdt())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/prdt/**"))
                .build()
                .groupName("产品管理")
                .pathMapping("/");
    }

    @Bean
    public Docket web_api_setaps() {
    
    
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/setaps/**"))
                .build()
                .groupName("基础制程配置")
                .pathMapping("/");
    }

    //
}

接下来把对应的接口归类如下:
在这里插入图片描述
接下来看效果
在这里插入图片描述
可以写apiInfoprdt 方法进行API描述。

文章技术含量不大,只是一个小技巧,原创不易,欢迎评论,转载请注明出处。

猜你喜欢

转载自blog.csdn.net/weixin_44690195/article/details/108011557