我正在使用swagger-ui创建一个nodejs Web服务。所以我继续使用swagger的在线编辑器,做了我的yaml并将其导出为nodejs服务器。 当我在我的计算机上运行时,将主机作为localhost(请参阅yaml)我尝试执行PUT我收到了该消息:
Error: Route defined in Swagger specification (/test) but there is no defined get operation.
at send405 (/home/guillaume/Desktop/docker-swagger2-amp/node_modules/swagger-tools/middleware/swagger-router.js:307:13)
at swaggerRouter (/home/guillaume/Desktop/docker-swagger2-amp/node_modules/swagger-tools/middleware/swagger-router.js:422:16)
at call (/home/guillaume/Desktop/docker-swagger2-amp/node_modules/connect/index.js:239:7)
at next (/home/guillaume/Desktop/docker-swagger2-amp/node_modules/connect/index.js:183:5)
at swaggerValidator (/home/guillaume/Desktop/docker-swagger2-amp/node_modules/swagger-tools/middleware/swagger-validator.js:409:14)
at call (/home/guillaume/Desktop/docker-swagger2-amp/node_modules/connect/index.js:239:7)
at next (/home/guillaume/Desktop/docker-swagger2-amp/node_modules/connect/index.js:183:5)
at swaggerMetadata (/home/guillaume/Desktop/docker-swagger2-amp/node_modules/swagger-tools/middleware/swagger-metadata.js:451:14)
at call (/home/guillaume/Desktop/docker-swagger2-amp/node_modules/connect/index.js:239:7)
at next (/home/guillaume/Desktop/docker-swagger2-amp/node_modules/connect/index.js:183:5)
和
Failed to load http://127.0.0.1:8080/v2/test: Response for preflight has invalid HTTP status code 405
我有一个获取操作,所以我不知道错误是什么。这是我的主要文件:
YAML:
swagger: "2.0"
info:
description: "This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters."
version: "1.0.0"
title: "Swagger Petstore"
termsOfService: "http://swagger.io/terms/"
contact:
email: "apiteam@swagger.io"
license:
name: "Apache 2.0"
url: "http://www.apache.org/licenses/LICENSE-2.0.html"
host: "127.0.0.1:8080"
basePath: "/v2"
tags:
- name: "test"
description: "Define yours parameters launching"
schemes:
- "http"
paths:
/test:
put:
tags:
- "test"
summary: "Add Test configuration and use it to launch Test"
description: ""
operationId: "addTestconf"
consumes:
- "application/xml"
- "application/json"
produces:
- "application/xml"
- "application/json"
parameters:
- in: "body"
name: "body"
description: "test configuration that needs to be used to run test"
required: true
schema:
$ref: "#/definitions/test"
responses:
400:
description: "Invalid ID supplied"
404:
description: "test not found"
405:
description: "Validation exception"
/test/{TestId}:
get:
tags:
- "test"
summary: "Find Test config by ID"
description: "Returns a Test config"
operationId: "getTestConfigurationById"
produces:
- "application/xml"
- "application/json"
parameters:
- name: "testId"
in: "path"
description: "ID of test config to return"
required: true
type: "integer"
format: "int64"
responses:
200:
description: "successful operation"
schema:
$ref: "#/definitions/test"
400:
description: "Invalid ID supplied"
404:
description: "test Config not found"
definitions: ........
index.js:
'use strict';
var fs = require('fs'),
path = require('path'),
http = require('http')
var app = require('connect')();
var swaggerTools = require('swagger-tools');
var jsyaml = require('js-yaml');
var serverPort = 8080;
// swaggerRouter configuration
var options = {
swaggerUi: path.join(__dirname, '/swagger.json'),
controllers: path.join(__dirname, './controllers'),
useStubs: process.env.NODE_ENV === 'development' // Conditionally turn on stubs (mock mode)
};
// The Swagger document (require it, build it programmatically, fetch it from a URL, ...)
var spec = fs.readFileSync(path.join(__dirname,'api/swagger.yaml'), 'utf8');
var swaggerDoc = jsyaml.safeLoad(spec);
// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
app.use(middleware.swaggerMetadata());
// Validate Swagger requests
app.use(middleware.swaggerValidator());
// Route validated requests to appropriate controller
app.use(middleware.swaggerRouter(options));
// Serve the Swagger documents and Swagger UI
app.use(middleware.swaggerUi());
// Start the server
http.createServer(app).listen(serverPort, function () {
console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
console.log('Swagger-ui is available on http://localhost:%d/docs', serverPort);
});
});
和我的控制器: 的 test_configuration.js:
'use strict';
var url = require('url');
var test = require('./testService');
module.exports.addTestConf = function addTestConf(req, res, next) {
test.addTestConf(req.swagger.params, res, next);
};
module.exports.getTestConfigurationById = function getTestConfigurationById(req, res, next) {
test.getTestConfigurationById(req.swagger.params, res, next);
};
test_configurationService.js:
use strict';
exports.addTestConf = function(args, res, next) {
res.end();
}
exports.getTestConfigurationById = function(args, res, next) {
res.end();
}
答案 0 :(得分:0)
operationId必须与控制器功能匹配。
看起来您的代码中存在大小写不匹配的情况:
答案 1 :(得分:0)
这是一个与 CORS 相关的问题
如果您尝试使用与 .yaml 文件中指示的不同的 url/host 请求 Api,您将收到此错误
所以如果在你的 .yaml 中你有
host: "localhost:8085"
并使用 127.0.0.1:8080 您可能会遇到此问题。