springdoc 使用

1、引入依赖



    org.springdoc
    springdoc-openapi-webmvc-core
    ${springdoc.version}


    org.springdoc
    springdoc-openapi-javadoc
    ${springdoc.version}

2、引入插件


    org.apache.maven.plugins
    maven-compiler-plugin
    3.9.0
    
        ${java.version}
        ${java.version}
        ${project.build.sourceEncoding}
        
            
                com.github.therapi
                therapi-runtime-javadoc-scribe
                0.15.0
            
            
                org.projectlombok
                lombok
                ${lombok.version}
            
            
                org.springframework.boot
                spring-boot-configuration-processor
                ${spring-boot.version}
            
        
    

3、配置类

@RequiredArgsConstructor
@Configuration
@AutoConfigureBefore(SpringDocConfiguration.class)
@ConditionalOnProperty(name = "springdoc.api-docs.enabled", havingValue = "true", matchIfMissing = true)
public class SwaggerConfig {

    private final SwaggerProperties swaggerProperties;
    private final ServerProperties serverProperties;

    @Bean
    @ConditionalOnMissingBean(OpenAPI.class)
    public OpenAPI openApi() {
        OpenAPI openApi = new OpenAPI();
        // 文档基本信息
        SwaggerProperties.InfoProperties infoProperties = swaggerProperties.getInfo();
        Info info = convertInfo(infoProperties);
        openApi.info(info);
        // 扩展文档信息
        openApi.externalDocs(swaggerProperties.getExternalDocs());
        openApi.tags(swaggerProperties.getTags());
        openApi.paths(swaggerProperties.getPaths());
        openApi.components(swaggerProperties.getComponents());
        Set keySet = swaggerProperties.getComponents().getSecuritySchemes().keySet();
        List list = new ArrayList<>();
        SecurityRequirement securityRequirement = new SecurityRequirement();
        keySet.forEach(securityRequirement::addList);
        list.add(securityRequirement);
        openApi.security(list);

        return openApi;
    }

    private Info convertInfo(SwaggerProperties.InfoProperties infoProperties) {
        Info info = new Info();
        info.setTitle(infoProperties.getTitle());
        info.setDescription(infoProperties.getDescription());
        info.setContact(infoProperties.getContact());
        info.setLicense(infoProperties.getLicense());
        info.setVersion(infoProperties.getVersion());
        return info;
    }

    /**
     * 自定义 openapi 处理器
     */
    @Bean
    public OpenAPIService openApiBuilder(Optional openAPI,
                                         SecurityService securityParser,
                                         SpringDocConfigProperties springDocConfigProperties, PropertyResolverUtils propertyResolverUtils,
                                         Optional> openApiBuilderCustomisers,
                                         Optional> serverBaseUrlCustomisers, Optional javadocProvider) {
        return new OpenApiHandler(openAPI, securityParser, springDocConfigProperties, propertyResolverUtils, openApiBuilderCustomisers, serverBaseUrlCustomisers, javadocProvider);
    }

    /**
     * 对已经生成好的 OpenApi 进行自定义操作
     */
    @Bean
    public OpenApiCustomiser openApiCustomiser() {
        String contextPath = serverProperties.getServlet().getContextPath();
        String finalContextPath;
        if (ObjectUtils.isEmpty(contextPath) || "/".equals(contextPath)) {
            finalContextPath = "";
        } else {
            finalContextPath = contextPath;
        }
        // 对所有路径增加前置上下文路径
        return openApi -> {
            Paths oldPaths = openApi.getPaths();
            if (oldPaths instanceof PlusPaths) {
                return;
            }
            PlusPaths newPaths = new PlusPaths();
            oldPaths.forEach((k,v) -> newPaths.addPathItem(finalContextPath + k, v));
            openApi.setPaths(newPaths);
        };
    }

    /**
     * 单独使用一个类便于判断 解决springdoc路径拼接重复问题
     *
     * @author
     */
    static class PlusPaths extends Paths {

        public PlusPaths() {
            super();
        }
    }

}

4、配置文件。在application.yml

# Swagger配置
swagger:
  info:
    # 标题
    title: '接口文档'
    # 描述
    description: '描述:用于公司信息'
    # 版本
    version: '版本号:1'
    # 作者信息
    contact:
      name: gxfc
      email: [email protected]
      url: https://www.baidu.com
  components:
    # 鉴权方式配置
    security-schemes:
      apiKey:
        type: APIKEY
        in: HEADER
        name: Authorization

springdoc:
  api-docs:
    # 是否开启接口文档
    enabled: true
  swagger-ui:
    # 持久化认证数据
    persistAuthorization: true
  #这里定义了两个分组,可定义多个,也可以不定义
  group-configs:
    #    - group: 1.演示模块
    #     http://localhost:8080/v3/api-docs/api
    - group: api
      packages-to-scan: com.xj.map

ok ,可以用了。

你可能感兴趣的:(java,前端,服务器)