swagger3.0设置分组和配置多个扫描路径和过滤URL

package com.jeeplus.config;

import com.fasterxml.classmate.TypeResolver;
import com.jeeplus.sys.service.dto.LightUserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.AlternateTypeRule;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.*;


/**
 * swagger配制
 */
@Configuration
public class SwaggerConfig implements WebMvcConfigurer {


    @Bean
    public Docket api() {//默认分组
        TypeResolver typeResolver=new TypeResolver();//过滤
        AlternateTypeRule alternateTypeRule=new AlternateTypeRule(typeResolver.resolve(LightUserDTO.class),typeResolver.resolve(Object.class));

        return new Docket ( DocumentationType.OAS_30 )
                //.groupName("开发组001")
                //资源
                .globalResponses ( HttpMethod.GET, new ArrayList <> ( ) )
                .globalResponses ( HttpMethod.PUT, new ArrayList <> ( ) )
                .globalResponses ( HttpMethod.POST, new ArrayList <> ( ) )
                .globalResponses ( HttpMethod.DELETE, new ArrayList <> ( ) )
                //是否启动
                .enable ( true )
                .alternateTypeRules(alternateTypeRule)
                //头部信息
                .apiInfo ( apiInfo ( ) )
                .select ( )
                /**
                 * RequestHandlerSelectors,配置要扫描接口的方式
                 * basePackage指定要扫描的包
                 * any()扫描所有,项目中的所有接口都会被扫描到
                 * none()不扫描
                 * withClassAnnotation()扫描类上的注解
                 * withMethodAnnotation()扫描方法上的注解
                 */
                .apis (
                        RequestHandlerSelectors.basePackage ( "com.jeeplus.duxin.basicsettings.bommaintenance" )
                )
                //加了ApiOperation注解的类,才生成接口文档
                .apis ( RequestHandlerSelectors.withClassAnnotation ( Api.class ) )
                .apis ( RequestHandlerSelectors.withMethodAnnotation ( ApiOperation.class ) )
                .apis ( RequestHandlerSelectors.any ( ) )
                //过滤某个路径
                .paths ( PathSelectors.any ( ) )
                .build ( )
                //协议
                .protocols ( newHashSet ( "https", "http" ) )
                .securitySchemes ( securitySchemes ( ) )
                .securityContexts ( securityContexts ( ) );
    }


    /**
     * 组织架构系统管理
     * @author lgn
     * @date 2023/10/25 18:20
     * @return Docket
     */
    @Bean
    public Docket api2() {
        TypeResolver typeResolver=new TypeResolver();
        AlternateTypeRule alternateTypeRule=new AlternateTypeRule(typeResolver.resolve(LightUserDTO.class),typeResolver.resolve(Object.class));

        return new Docket ( DocumentationType.OAS_30 )
                .groupName("组织架构系统管理")
                //资源
                .globalResponses ( HttpMethod.GET, new ArrayList <> ( ) )
                .globalResponses ( HttpMethod.PUT, new ArrayList <> ( ) )
                .globalResponses ( HttpMethod.POST, new ArrayList <> ( ) )
                .globalResponses ( HttpMethod.DELETE, new ArrayList <> ( ) )
                //是否启动
                .enable ( true )
                .alternateTypeRules(alternateTypeRule)
                //头部信息
                .apiInfo ( apiInfo ( ) )
                .select ( )
                /**
                 * RequestHandlerSelectors,配置要扫描接口的方式
                 * basePackage指定要扫描的包
                 * any()扫描所有,项目中的所有接口都会被扫描到
                 * none()不扫描
                 * withClassAnnotation()扫描类上的注解
                 * withMethodAnnotation()扫描方法上的注解
                 */
/*                .apis (
                        RequestHandlerSelectors.basePackage ( "com.jeeplus.duxin.basicsettings.basestamp" )
                )*/
                .apis(
                        RequestHandlerSelectors.basePackage("com.jeeplus.duxin.basicsettings.financeabstract")
                                .or(RequestHandlerSelectors.basePackage("com.jeeplus.duxin.basicsettings.financeaccountingsubject"))
                                .or(RequestHandlerSelectors.basePackage("com.jeeplus.duxin.basicsettings.financecalculateproject"))
                                .or(RequestHandlerSelectors.basePackage("com.jeeplus.duxin.basicsettings.financecashflow"))

                )

                //加了ApiOperation注解的类,才生成接口文档
                .apis ( RequestHandlerSelectors.withClassAnnotation ( Api.class ) )
                .apis ( RequestHandlerSelectors.withMethodAnnotation ( ApiOperation.class ) )
                .apis ( RequestHandlerSelectors.any ( ) )
                //过滤某个路径
                .paths ( PathSelectors.any ( ) )
                .build ( )
                //协议
                .protocols ( newHashSet ( "https", "http" ) )
                .securitySchemes ( securitySchemes ( ) )
                .securityContexts ( securityContexts ( ) );
    }


    /**
     * 财务管理
     * @author lgn
     * @date 2023/10/25 18:20
     * @return Docket
     */
    @Bean
    public Docket api3() {
        TypeResolver typeResolver=new TypeResolver();
        AlternateTypeRule alternateTypeRule=new AlternateTypeRule(typeResolver.resolve(LightUserDTO.class),typeResolver.resolve(Object.class));

        return new Docket ( DocumentationType.OAS_30 )
                .groupName("财务管理")
                //资源
                .globalResponses ( HttpMethod.GET, new ArrayList <> ( ) )
                .globalResponses ( HttpMethod.PUT, new ArrayList <> ( ) )
                .globalResponses ( HttpMethod.POST, new ArrayList <> ( ) )
                .globalResponses ( HttpMethod.DELETE, new ArrayList <> ( ) )
                //是否启动
                .enable ( true )
                .alternateTypeRules(alternateTypeRule)
                //头部信息
                .apiInfo ( apiInfo ( ) )
                .select ( )
                /**
                 * RequestHandlerSelectors,配置要扫描接口的方式
                 * basePackage指定要扫描的包
                 * any()扫描所有,项目中的所有接口都会被扫描到
                 * none()不扫描
                 * withClassAnnotation()扫描类上的注解
                 * withMethodAnnotation()扫描方法上的注解
                 */
                .apis (
                        RequestHandlerSelectors.basePackage("com.jeeplus.duxin.basicsettings.financeabstract")
                                .or(RequestHandlerSelectors.basePackage("com.jeeplus.duxin.basicsettings.financeaccountingsubject"))
                                .or(RequestHandlerSelectors.basePackage("com.jeeplus.duxin.basicsettings.financecalculateproject"))
                                .or(RequestHandlerSelectors.basePackage("com.jeeplus.duxin.basicsettings.financecashflow"))
                                .or(RequestHandlerSelectors.basePackage("com.jeeplus.duxin.basicsettings.financecashflowcategory"))
                                .or(RequestHandlerSelectors.basePackage("com.jeeplus.duxin.basicsettings.financeotherunit"))
                                .or(RequestHandlerSelectors.basePackage("com.jeeplus.duxin.basicsettings.financeotherunitbookkeep"))
                                .or(RequestHandlerSelectors.basePackage("com.jeeplus.duxin.basicsettings.financeotherunitbookkeepcategory"))
                                .or(RequestHandlerSelectors.basePackage("com.jeeplus.duxin.basicsettings.financeregularsubject"))
                                .or(RequestHandlerSelectors.basePackage("com.jeeplus.duxin.basicsettings.financestandardsubject"))
                                .or(RequestHandlerSelectors.basePackage("com.jeeplus.duxin.basicsettings.financestockbookkeep"))
                                .or(RequestHandlerSelectors.basePackage("com.jeeplus.duxin.basicsettings.financestockbookkeepcategory"))
                                .or(RequestHandlerSelectors.basePackage("com.jeeplus.duxin.basicsettings.financesubjectappoint"))
                                .or(RequestHandlerSelectors.basePackage("com.jeeplus.duxin.basicsettings.financevoucher"))
                )

                //加了ApiOperation注解的类,才生成接口文档
                .apis ( RequestHandlerSelectors.withClassAnnotation ( Api.class ) )
                .apis ( RequestHandlerSelectors.withMethodAnnotation ( ApiOperation.class ) )
                .apis ( RequestHandlerSelectors.any ( ) )
                //过滤某个路径
                .paths ( PathSelectors.any ( ) )
                .build ( )
                //协议
                .protocols ( newHashSet ( "https", "http" ) )
                .securitySchemes ( securitySchemes ( ) )
                .securityContexts ( securityContexts ( ) );
    }


    /**
     * API 页面上半部分展示信息
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder ( )
                .title ( "Jeeplus微服务接口文档" )
                .description ( "@author jeeplus" )
                .contact ( new Contact ( "jeeplus", "http://www.jeeplus.org", "[email protected]" ) )
                .version ( "9.0" )
                .build ( );
    }

    /**
     * 设置接口单独的授权信息
     */
    private List  securitySchemes() {
        return Collections.singletonList ( new ApiKey ( "token", "token", "header" ) );
    }

    /**
     * 授权信息全局应用
     */
    private List  securityContexts() {
        return Collections.singletonList (
                SecurityContext.builder ( )
                        .securityReferences (
                                Collections.singletonList ( new SecurityReference ( "token",
                                        new AuthorizationScope[]{new AuthorizationScope ( "global", "" )}
                                ) ) )
                        .build ( )
        );
    }

    @SafeVarargs
    private final  Set  newHashSet(T... ts) {
        if ( ts.length > 0 ) {
            return new LinkedHashSet <> ( Arrays.asList ( ts ) );
        }
        return null;
    }
}



这里是排除不需要的返回给swagger的 对象:
swagger3.0设置分组和配置多个扫描路径和过滤URL_第1张图片
这块是设置多个分组:
swagger3.0设置分组和配置多个扫描路径和过滤URL_第2张图片
这块是配置多个扫描路径:

swagger3.0设置分组和配置多个扫描路径和过滤URL_第3张图片

你可能感兴趣的:(Java,常见工具类,常见错误解决方法,java)