SpringFox 3.0.0内部使用swagger v2,但在本地主机上可以下载swaggerUI openAPI v3文件

问题描述 投票:0回答:1

我会解释这个问题。

我编写了以下代码以在我的 SpringBoot 项目中生成静态文件:

package com.kovospace.paster.base.swagger;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.models.Swagger;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.OpenAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import springfox.documentation.service.Documentation;
import springfox.documentation.spring.web.DocumentationCache;
import springfox.documentation.swagger2.mappers.ServiceModelToSwagger2Mapper;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.stream.Stream;

import io.swagger.v3.core.util.Yaml;


import static com.kovospace.paster.base.utils.Utils.exceptionHandler;

@Component
public class DocumentGenerator implements CommandLineRunner {

    @Value("${app.swagger.generated.files.path}")
    private String storagePath;

    private DocumentationCache documentationCache;
    private ServiceModelToSwagger2Mapper mapper;
    private ObjectMapper objectMapper;

    @Autowired
    public DocumentGenerator(
            final DocumentationCache documentationCache,
            final ServiceModelToSwagger2Mapper mapper,
            final ObjectMapper objectMapper
    ) {
        this.documentationCache = documentationCache;
        this.mapper = mapper;
        this.objectMapper = objectMapper;
    }

    @Override
    public void run(String... args) throws IOException {
    Optional.ofNullable(documentationCache.all())
                .map(docMap -> docMap.entrySet())
                .map(docEntries -> docEntries.stream())
                .orElseGet(Stream::empty)
                .forEach(docEntry -> generateAllexpectedDocumentTypes(docEntry));
    }

    private void generateAllexpectedDocumentTypes(final Map.Entry<String, Documentation> documentation) {
        Arrays.stream(ExpectedDocumentType.values())
                .forEach(documentType -> {
                    final String outputFilePath = getOutputFilePath(documentation, documentType.extension());
                    final String fileContent = converter.apply(documentation, documentType);
                    writeFile(fileContent, outputFilePath);
                });
    }

    private String getOutputFilePath(final Map.Entry<String, Documentation> documentation, final String extension) {
        return StringUtils.isEmpty(storagePath)
                ? String.format("%s.%s", documentation.getKey(), extension)
                : String.format("%s/%s.%s", storagePath, documentation.getKey(), extension);
    }

    private void writeFile(final String document, final String filePath) {
        final File outputFile = new File(filePath);
        if (!outputFile.exists()) {
            outputFile.getParentFile().mkdirs();
            try {
                outputFile.createNewFile();
            } catch (IOException e) {
                //TODO
                throw new RuntimeException(e);
            }
        }
        try (FileWriter fileWriter = new FileWriter(outputFile)) {
            fileWriter.write(document);
        } catch (Exception e) {
            //TODO
        }
    }

    private BiFunction<Map.Entry<String, Documentation>, ExpectedDocumentType, String> converter =
            (documentationEntry, expectedDocumentType) -> {
                final Swagger swagger = mapper.mapDocumentation(
                        documentationCache.documentationByGroup(documentationEntry.getKey()));
                switch (expectedDocumentType) {
                    case JSON:
                        return exceptionHandler(() -> Json.pretty(swagger));
                    case YAML:
                    default:
                        return exceptionHandler(() -> Yaml.pretty().writeValueAsString(swagger));
                }
            };
}

并且它正在生成 swagger v2 格式的文件:

swagger: "2.0"
info:
  description: Api Documentation
  version: "1.0"
  title: Api Documentation
  termsOfService: urn:tos
  contact:
    name: ""
    url: ""
    email: ""
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0
host: ""
basePath: /
tags:
- name: info-controller
  description: Info Controller
- name: item-controller
  description: Item Controller
- name: user-controller
  description: User Controller
schemes: []
consumes: []
produces:
- application/json
paths:
  /api/info/checkPlatform:
    post:
      tags:
      - info-controller
      summary: Check Platform
      description: Given platform is listed and supported

尽管我将其设置为 openAPI v3:

package com.kovospace.paster.base.configurations;

import com.kovospace.paster.base.annotations.swagger.PublicEndpoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.HashSet;

@Configuration
public class SwaggerConfig {

    //TODO do konstant alebo properties
    private static final String BASE_PACKAGE = "com.kovospace.paster";
    private static final String PUBLIC_API_GROUPNAME = "public-api";
    private static final String PRIVATE_API_GROUPNAME = "devel-api";
    private static final HashSet<String> RETURN_TYPES_POSSIBLE = new HashSet<String>() {{
        add(MediaType.APPLICATION_JSON_VALUE);
    }};

    @Bean
    public Docket publicApi() {
        return new Docket(DocumentationType.OAS_30)
                .groupName(PUBLIC_API_GROUPNAME)
                .produces(RETURN_TYPES_POSSIBLE)
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(PublicEndpoint.class))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket developerApi() {
        return new Docket(DocumentationType.OAS_30)
                .groupName(PRIVATE_API_GROUPNAME)
                .produces(RETURN_TYPES_POSSIBLE)
                .select()
                .apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }

}

但是你猜怎么着?当我在本地主机上的浏览器中打开 swaggerUI 时,我可以下载声称它是 V3 的文件:

 {
openapi: "3.0.3",
info: {
title: "Api Documentation",
description: "Api Documentation",
termsOfService: "urn:tos",
contact: { },
license: {
name: "Apache 2.0",
url: "http://www.apache.org/licenses/LICENSE-2.0"
},
version: "1.0"
},
servers: [
{
url: "https://0.0.0.0:4004",
description: "Inferred Url"
}
],
tags: [
{
name: "info-controller",
description: "Info Controller"
},
{
name: "item-controller",
description: "Item Controller"
},
{
name: "user-controller",
description: "User Controller"
}
],
paths: {
/api/info/checkPlatform: {
post: {
tags: [
"info-controller"
],
summary: "Check Platform",
description: "Given platform is listed and supported",

真相在哪里,为什么我的项目文件夹中无法生成漂亮的 v3 文件,或者我在 swaggerUI 前端上有假的 v3.0 文件? 我已经在控制器端点上使用 v3.0 注释,可能出了什么问题?

spring-boot swagger openapi springfox springfox-boot-starter
1个回答
0
投票

抱歉打扰,但答案是:我瞎了:

private ServiceModelToSwagger2Mapper mapper;
更改为
private ServiceModelToOpenApiMapper mapper;

需要1MD

© www.soinside.com 2019 - 2024. All rights reserved.