Java 解析API Swagger格式文档

maven 依赖

        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.21</version>
        </dependency>

import 类和方法

import io.swagger.models.*;
import io.swagger.models.auth.SecuritySchemeDefinition;
import io.swagger.models.parameters.BodyParameter;
import io.swagger.models.parameters.Parameter;
import io.swagger.models.parameters.PathParameter;
import io.swagger.models.parameters.QueryParameter;
import io.swagger.models.properties.*;

具体方法

**
     * @param swagger_json_file
     * @param               JSON 或者 YAML
     * @return
     */
    public Swagger getSwaggerFromFile(String swagger_json_file) {
        JSONObject root = JSONObject.parseObject(swagger_json_file, Feature.OrderedField);
//        JSONObject root2 = jsonObject.parseObject(swagger_json_file, JSONObject.class, Feature.OrderedField);
        String swagger = root.getString("swagger");
        Info info = JsonUtil.parseFormatSort(root.getString("info"), Info.class);
        String host = root.getString("host");
        String basePath = root.getString("basePath");
        List<Tag> tags = new ArrayList<>();
        if (!ObjectUtils.isEmpty(root.getJSONArray("tags"))) {
            tags = JsonUtil.parseArrFormat(root.getJSONArray("tags").toJSONString(), Tag.class);
        }
        List<Scheme> schemes = JsonUtil.parseArrFormat(root.getString("schemes"), Scheme.class);
        List<String> consumes = JsonUtil.parseArrFormat(root.getString("consumes"), String.class);
        List<String> produces = JsonUtil.parseArrFormat(root.getString("produces"), String.class);
        List<SecurityRequirement> security = JsonUtil.parseArrFormat(root.getString("security"), SecurityRequirement.class);
        //
        Map<String, Model> definitions = new LinkedHashMap<>();
        //
        Map<String, Path> paths = new LinkedHashMap<>();
        Map<String, JSONObject> pathsRoot = JsonUtil.parseFormatSort(root.getString("paths"), Map.class);
        getPaths(pathsRoot, paths, definitions);

        //
        Map<String, SecuritySchemeDefinition> securityDefinitions = new LinkedHashMap<>();
        //
        Map<String, JSONObject> definitionsRoot = JsonUtil.parseFormatSort(root.getString("definitions"), Map.class);
        definitions = this.getDefinitionsSwagger(definitionsRoot, definitions);

        //
        Map<String, Parameter> parameters = new LinkedHashMap<>();
        Map<String, JSONObject> parametersRoot = JsonUtil.parseFormatSort(root.getString("parameters"), Map.class);
        if (!ObjectUtils.isEmpty(parametersRoot)) {
            for (Map.Entry<String, JSONObject> e1 : parametersRoot.entrySet()) {
                String mapKey = e1.getKey();
                JSONObject mapValue = e1.getValue();
                Parameter opMap = JsonUtil.parseFormatSort(mapValue.toJSONString(), Parameter.class);
                parameters.put(mapKey, opMap);
            }
        }

        //
        Map<String, Response> responses = new LinkedHashMap<>();
        Map<String, JSONObject> responsesRoot = JsonUtil.parseFormatSort(root.getString("responses"), Map.class);
        if (!ObjectUtils.isEmpty(responsesRoot)) {
            for (Map.Entry<String, JSONObject> e1 : responsesRoot.entrySet()) {
                String mapKey = e1.getKey();
                JSONObject mapValue = e1.getValue();
                Response opMap = JsonUtil.parseFormatSort(mapValue.toJSONString(), Response.class);
                responses.put(mapKey, opMap);
            }
        }

        //
        ExternalDocs externalDocs = JsonUtil.parseFormatSort(root.getString("externalDocs"), ExternalDocs.class);

        //
        Map<String, Object> vendorExtensions = new LinkedHashMap<>();
        Map<String, Object> vendorExtensionsRoot = JsonUtil.parseFormatSort(root.getString("vendorExtensions"), Map.class);
        if (!ObjectUtils.isEmpty(vendorExtensionsRoot)) {
            for (Map.Entry<String, Object> e1 : vendorExtensionsRoot.entrySet()) {
                String mapKey = e1.getKey();
                Object mapValue = e1.getValue();
                vendorExtensions.put(mapKey, mapValue);
            }
        }

        //
        Swagger s = new Swagger();
        s.setSwagger(swagger);
        s.setInfo(info);
        s.setHost(host);
        s.setBasePath(basePath);
        s.setTags(tags);
        s.setSchemes(schemes);
        s.setConsumes(consumes);
        s.setProduces(produces);
        s.setSecurity(security);
        s.setPaths(paths);
        s.setSecurityDefinitions(securityDefinitions);
        s.setDefinitions(definitions);
        s.setParameters(parameters);
        s.setResponses(responses);
        s.setExternalDocs(externalDocs);
        s.setVendorExtensions(vendorExtensions);

        return s;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值