Swagger编辑器:错误,未找到服务器或发生错误。

4

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

Swagger-UI中,预览看起来正确,即Content-Type: application/json,并且JSON位于正文中。但是当我从Swagger编辑器向服务器发送GET请求时,出现了错误。

ERROR Server not found or an error occurred

我的Swagger

{
    "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"
                }
            }
        }
    }
}

请帮我。

提前感谢。


1
假设您的服务器响应了您配置的内容:http://127.0.0.1:8610/api/v1? - fehguy
3个回答

1
我已经找到解决方法。 这是跨域资源共享(CORS)问题。我的浏览器正在阻止CORS请求。我安装了一个Chrome扩展程序,可以在发送的请求中添加Access-Control-Allow-Origin。

1
我已经下载了,但仍然无法允许请求。之后还需要做些什么吗? - thisisdude

1
请确保您的服务器正在运行。
如果您已经安装了Swagger,可以执行以下操作:
swagger project start

0

我遇到了类似的问题

这是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());

希望有帮助

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接