Swagger 编辑器:找不到错误服务器或发生错误

Swagger Editor : ERROR Server not found or an error occurred

我是 Swagger 工具的新手。我尝试使用 swagger 编辑器 测试我的 Restfull 应用程序。我使用基本身份验证来访问 Web 服务。

Swagger-UI 中,预览看起来是正确的,即 Content-Type: application/json 并且 json 在body.But 当我从 Swagger 编辑器向服务器发送 GET 请求时,出现错误。

ERROR Server not found or an error occurred

我的招摇

{
    "swagger": "2.0",
    "info": {
        "version": "1.0.0",
        "title": "Swagger Petstore (Simple)",
        "description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
        "termsOfService": "http://helloreverb.com/terms/",
        "contact": {
            "name": "Swagger API team",
            "email": "abc@gmail.com",
            "url": "http://avfg.com"
        },
        "license": {
            "name": "MIT",
            "url": "http://opensource.org/licenses/MIT"
        }
    },
    "host": "127.0.0.1:8xxx",
    "basePath": "/v1",
    "schemes": [
        "http"
    ],
    "consumes": [
        "application/json"
    ],
    "produces": [
        "application/json"
    ],
    "paths": {
        "/facedetect/{username}/{albumname}/{imagename}": {
            "get": {
                "description": "Returns all pets from the system that the user has access to",
                "operationId": "findPets",
                "produces": [
                    "application/json",
                    "application/xml"
                ],
                "parameters": [
                    {
                        "name": "username",
                        "in": "path",
                        "description": "tags to filter by",
                        "required": true,
                        "type": "string"
                    },
                    {
                        "name": "albumname",
                        "in": "path",
                        "description": "maximum number of results to return",
                        "required": true,
                        "type": "string"
                    },
                    {
                        "name": "imagename",
                        "in": "path",
                        "description": "maximum number of results to return",
                        "required": true,
                        "type": "string"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "pet response",
                        "schema": {
                            "type": "array",
                            "items": {
                                "$ref": "#/definitions/pet"
                            }
                        }
                    },
                    "default": {
                        "description": "unexpected error",
                        "schema": {
                            "$ref": "#/definitions/errorModel"
                        }
                    }
                }
            }
        }
    },
    "definitions": {
        "pet": {
            "type": "object",
            "required": [
                "id",
                "name"
            ],
            "properties": {
                "id": {
                    "type": "integer",
                    "format": "int64"
                },
                "name": {
                    "type": "string"
                },
                "tag": {
                    "type": "string"
                }
            }
        },
        "errorModel": {
            "type": "object",
            "required": [
                "code",
                "message"
            ],
            "properties": {
                "code": {
                    "type": "integer",
                    "format": "int32"
                },
                "message": {
                    "type": "string"
                }
            }
        }
    }
}

请帮帮我

提前致谢。

确保您有服务器 运行。

如果你安装了swagger,你可以做

swagger project start

我得到了解决方案。

它的 CORS 问题。我的浏览器阻止了 cors 请求。我已经安装了一个 Chrome 扩展程序,它将 Access-Control-Allow-Origin 添加到传出请求中。

我遇到了类似的问题

这是 CORS,但是:

我在 nodeJS 中设置了:

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
  res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, UPDATE, DELETE, OPTIONS')
  res.header("Content-Type", "application/json");
  next();
});

但它仅在请求未经授权时有用(api-key)。为了让它工作,我必须改变并使用:

const cors = require('cors');
app.use(cors());

希望对您有所帮助