首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swagger规范中定义的Swagger路由,但没有定义get操作。

Swagger规范中定义的Swagger路由,但没有定义get操作。
EN

Stack Overflow用户
提问于 2018-01-24 08:22:15
回答 2查看 3.6K关注 0票数 7

我正在使用swagger创建一个nodejs web服务。于是,我使用了swagger的在线编辑器,完成了我的yaml,并将其导出到nodejs服务器。当我在我的计算机上运行时,主机作为本地主机(参见yaml),我尝试执行一个PUT --我得到了这样的消息:

代码语言:javascript
复制
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)

代码语言:javascript
复制
Failed to load http://127.0.0.1:8080/v2/test: Response for preflight has invalid HTTP status code 405

我有一个get操作,所以我不知道是什么错误。以下是我的主要文件:

YAML:

代码语言:javascript
复制
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:

代码语言:javascript
复制
'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);
      });

    });

和my控制器:

代码语言:javascript
复制
'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:

代码语言:javascript
复制
use strict';

exports.addTestConf = function(args, res, next) {
  res.end();
}

exports.getTestConfigurationById = function(args, res, next) {
  res.end();
}
EN

回答 2

Stack Overflow用户

发布于 2020-12-16 23:12:14

operationId必须与控制器函数匹配。

看起来,您的代码中存在一种情况不匹配:

  • operationId: addTestconf
  • 功能名称: addTestConf
票数 0
EN

Stack Overflow用户

发布于 2021-07-13 10:17:08

这是与CORS相关的问题。

如果尝试使用与.yaml文件中指示的不同的url/主机请求Api,则会得到以下错误

所以如果在你的.yaml里

代码语言:javascript
复制
host: "localhost:8085"

并使用127.0.0.1:8080,您可能会面临这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48417779

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档