如何摆脱swagger中的错误-没有定义的后期操作.",“allowedMethods":[>“获取"] } [英] How to get rid of the error in swagger - there is no defined post operation.", "allowedMethods": [ > "GET" ] }

查看:39
本文介绍了如何摆脱swagger中的错误-没有定义的后期操作.",“allowedMethods":[>“获取"] }的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码运行 swagger 并在执行 POST 操作时出错..

I am running swagger with following code and getting error while performing POST operation..

swagger: "2.0"
info:
  version: "0.0.1"
  title: Hello World App
# during dev, should point to your local machine
host: localhost:10010
# basePath prefixes all resource paths 
basePath: /
# 
schemes:
  # tip: remove http to make production-grade
  - http
  - https
# format of bodies a client can send (Content-Type)
consumes:
  - application/json
# format of the responses to the client (Accepts)
produces:
  - application/json
paths:
  /hello:
    # binds a127 app logic to a route
    x-swagger-router-controller: hello_world
    get:
      description: Returns 'Hello' to the caller
      # used as the method name of the controller
      operationId: hello
      parameters:
        - name: name
          in: query
          description: The name of the person to whom to say hello
          required: false
          type: string
      responses:
        "200":
          description: Success
          schema:
            # a pointer to a definition
            $ref: "#/definitions/HelloWorldResponse"
        # responses may fall through to errors
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
  /postpath:
    x-swagger-router-controller: hello_world

    post:
      description: add a new movie to the list
      # movie info to be stored
      operationId: postpath
      parameters:
        - name: title
          description: Movie properties
          in: body
          required: true
          schema:
            $ref: "#/definitions/Movie"
      responses:
        "200":
          description: Success
          schema:
            $ref: "#/definitions/GeneralResponse"
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"        
  /swagger:
    x-swagger-pipe: swagger_raw
# complex objects have schema definitions
definitions:
  HelloWorldResponse:
    required:
      - message
    properties:
      message:
        type: string
  ErrorResponse:
    required:
      - message
    properties:
      message:
        type: string
  Movie:
    type: object
    properties:
      title:
        type: string
        description: task object name
      year:
        type: number
        description: task description
    required:
      - title
      - year
  GeneralResponse:
    type: object
    properties:
      success:
        type: number
        description: returns 1 if successful
      description:
        type: string
        description: a short comment 
    required:
      - success
      - description

以下是其背后的 hello_world.js.

Following is the hello_world.js behind that.

'use strict';



var util = require('util');


module.exports = {
  hello: hello, postpath: postpath
};


function hello(req, res) {
  // variables defined in the Swagger document can be referenced using req.swagger.params.{parameter_name}
  var name = req.swagger.params.name.value || 'stranger';
  var hello = util.format('Hello, %s!', name, 'how are you?');

  // this sends back a JSON response which is a single string
  res.json(hello);
}

function postpath(req, res) {

var isOpen = true; // Details omitted
var doorStatus = isOpen;

res.json('Test');
}

我得到方法不允许的异常..

I am getting method not allowed exception..

以下是堆栈跟踪..

{ "message": "Swagger 规范中定义的路由 (/hello) 但是没有定义的后期操作.", "allowedMethods": [获取"] }

{ "message": "Route defined in Swagger specification (/hello) but there is no defined post operation.", "allowedMethods": [ "GET" ] }

推荐答案

您已经定义了:

paths:
  /hello:
    get:

但您还想定义 POST:

but you want to also define POST:

paths:
  /hello:
    get:
        - something
    post:
        - something else

这篇关于如何摆脱swagger中的错误-没有定义的后期操作.",“allowedMethods":[>“获取"] }的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆