java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot swagger 接口分组

springboot swagger 接口文档分组展示功能实现

作者:狄龙疤

这篇文章主要介绍了springboot swagger 接口文档分组展示功能实现,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧

例如将 controller 分成四类,分别放到四个包下:

        
xxx.xxx.xxx.controller.manage
xxx.xxx.xxx.controller.client
xxx.xxx.xxx.controller.authority
xxx.xxx.xxx.controller.common

SwaggerConfig.java:

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@RefreshScope
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Value("${system.swagger_show}")
    private boolean swaggerShow;
     @Bean
    public Docket manageGroup() {
        return this.buildDocket( "管理端接口","xxx.xxx.xxx.controller.manage" );
    }
    @Bean
    public Docket clientGroup() {
        return this.buildDocket( "移动端接口","xxx.xxx.xxx.controller.client" );
    }
    @Bean
    public Docket authorityGroup() {
        return this.buildDocket( "权限相关接口","xxx.xxx.xxx.controller.authority" );
    }
    @Bean
    public Docket commonGroup() {
        return this.buildDocket( "数据字典、行政区域公共接口","xxx.xxx.xxx.controller.common" );
    }
    private Docket buildDocket( String groupName,String basePackage ){
        return new Docket(DocumentationType.SWAGGER_2)
                    .groupName( groupName )
                    .enable(this.swaggerShow)
                    .produces(Collections.singleton(MediaType.APPLICATION_JSON_VALUE))
                    .genericModelSubstitutes(ResponseEntity.class)
                    .forCodeGeneration(false)
                    .useDefaultResponseMessages(false)
                    .pathMapping("/")
                    .select()
                    .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                    .apis(RequestHandlerSelectors.basePackage( basePackage ))
                    .paths(PathSelectors.any())
                    .build()
                    .globalOperationParameters( setHeaderToken() )
                    .directModelSubstitute(java.sql.Timestamp.class, java.sql.Date.class)
                    .apiInfo(apiInfo());
    }
    private List<Parameter> setHeaderToken() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("witToken").description("登录token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        return pars;
    }
    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("xxx系统( xxx) API")
                .description("xxx系统( xxx) API")
                .license(null)
                .licenseUrl(null)
                .contact(new Contact("developer", "", "developer@xxx.com"))
                .version("1.0")
                .build();
        return apiInfo;
    }
}
 

效果:

到此这篇关于springboot swagger 接口文档分组展示的文章就介绍到这了,更多相关springboot swagger 接口文档内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文