草庐IT

Golang swagger 返回 404 not found

coder 2024-07-10 原文

我遵循了这个 github 页面上的说明 https://github.com/yvasiyarov/swagger使用示例项目和 api 文档生成的很好。但是当我运行最后一个命令来运行并显示 API 页面时,我去那里时总是得到 404 not found

web.go 文件本质上是 https://github.com/yvasiyarov/swagger/blob/master/web.go-example .它基本上是 repo 中 web.go-example 的副本......我不明白为什么它会返回 404。

另外,您对类似的 api 文档生成器有什么更好的建议吗?我不想要像 https://github.com/go-swagger/go-swagger 这样的代码生成器.我想根据我现有的代码生成 api

这里是生成的docs.go

package main
//This file is generated automatically. Do not try to edit it manually.

var resourceListingJson = `{
    "apiVersion": "1.0.0",
    "swaggerVersion": "1.2",
    "basePath": "http://127.0.0.1:3000/",
    "apis": [
        {
            "path": "/testapi",
            "description": "Test API"
        }
    ],
    "info": {
        "title": "Swagger Example API",
        "description": "Swagger Example API",
        "contact": "varyous@gmail.com",
        "termsOfServiceUrl": "http://yvasiyarov.com/",
        "license": "BSD",
        "licenseUrl": "http://yvasiyarov.com/"
    }
}`
var apiDescriptionsJson = map[string]string{"testapi":`{
    "apiVersion": "1.0.0",
    "swaggerVersion": "1.2",
    "basePath": "http://127.0.0.1:3000/",
    "resourcePath": "/testapi",
    "produces": [
        "application/json"
    ],
    "apis": [
        {
            "path": "/testapi/get-string-by-int/{some_id}",
            "description": "get string by ID",
            "operations": [
                {
                    "httpMethod": "GET",
                    "nickname": "GetStringByInt",
                    "type": "string",
                    "items": {},
                    "summary": "get string by ID",
                    "parameters": [
                        {
                            "paramType": "path",
                            "name": "some_id",
                            "description": "Some ID",
                            "dataType": "int",
                            "type": "int",
                            "format": "",
                            "allowMultiple": false,
                            "required": true,
                            "minimum": 0,
                            "maximum": 0
                        }
                    ],
                    "responseMessages": [
                        {
                            "code": 200,
                            "message": "",
                            "responseType": "object",
                            "responseModel": "string"
                        },
                        {
                            "code": 400,
                            "message": "We need ID!!",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.APIError"
                        },
                        {
                            "code": 404,
                            "message": "Can not find ID",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.APIError"
                        }
                    ],
                    "produces": [
                        "application/json"
                    ]
                }
            ]
        },
        {
            "path": "/testapi/get-struct-by-int/{some_id}",
            "description": "get struct by ID",
            "operations": [
                {
                    "httpMethod": "GET",
                    "nickname": "GetStructByInt",
                    "type": "git.corp.adobe.com.personal.test1.StructureWithEmbededStructure",
                    "items": {},
                    "summary": "get struct by ID",
                    "parameters": [
                        {
                            "paramType": "path",
                            "name": "some_id",
                            "description": "Some ID",
                            "dataType": "int",
                            "type": "int",
                            "format": "",
                            "allowMultiple": false,
                            "required": true,
                            "minimum": 0,
                            "maximum": 0
                        },
                        {
                            "paramType": "query",
                            "name": "offset",
                            "description": "Offset",
                            "dataType": "int",
                            "type": "int",
                            "format": "",
                            "allowMultiple": false,
                            "required": true,
                            "minimum": 0,
                            "maximum": 0
                        },
                        {
                            "paramType": "query",
                            "name": "limit",
                            "description": "Offset",
                            "dataType": "int",
                            "type": "int",
                            "format": "",
                            "allowMultiple": false,
                            "required": true,
                            "minimum": 0,
                            "maximum": 0
                        }
                    ],
                    "responseMessages": [
                        {
                            "code": 200,
                            "message": "",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.StructureWithEmbededStructure"
                        },
                        {
                            "code": 400,
                            "message": "We need ID!!",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.APIError"
                        },
                        {
                            "code": 404,
                            "message": "Can not find ID",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.APIError"
                        }
                    ],
                    "produces": [
                        "application/json"
                    ]
                }
            ]
        },
        {
            "path": "/testapi/get-struct2-by-int/{some_id}",
            "description": "get struct2 by ID",
            "operations": [
                {
                    "httpMethod": "GET",
                    "nickname": "GetStruct2ByInt",
                    "type": "git.corp.adobe.com.personal.test1.StructureWithEmbededPointer",
                    "items": {},
                    "summary": "get struct2 by ID",
                    "parameters": [
                        {
                            "paramType": "path",
                            "name": "some_id",
                            "description": "Some ID",
                            "dataType": "int",
                            "type": "int",
                            "format": "",
                            "allowMultiple": false,
                            "required": true,
                            "minimum": 0,
                            "maximum": 0
                        },
                        {
                            "paramType": "query",
                            "name": "offset",
                            "description": "Offset",
                            "dataType": "int",
                            "type": "int",
                            "format": "",
                            "allowMultiple": false,
                            "required": true,
                            "minimum": 0,
                            "maximum": 0
                        },
                        {
                            "paramType": "query",
                            "name": "limit",
                            "description": "Offset",
                            "dataType": "int",
                            "type": "int",
                            "format": "",
                            "allowMultiple": false,
                            "required": true,
                            "minimum": 0,
                            "maximum": 0
                        }
                    ],
                    "responseMessages": [
                        {
                            "code": 200,
                            "message": "",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.StructureWithEmbededPointer"
                        },
                        {
                            "code": 400,
                            "message": "We need ID!!",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.APIError"
                        },
                        {
                            "code": 404,
                            "message": "Can not find ID",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.APIError"
                        }
                    ],
                    "produces": [
                        "application/json"
                    ]
                }
            ]
        },
        {
            "path": "/testapi/get-simple-array-by-string/{some_id}",
            "description": "get simple array by ID",
            "operations": [
                {
                    "httpMethod": "GET",
                    "nickname": "GetSimpleArrayByString",
                    "type": "array",
                    "items": {
                        "type": "string"
                    },
                    "summary": "get simple array by ID",
                    "parameters": [
                        {
                            "paramType": "path",
                            "name": "some_id",
                            "description": "Some ID",
                            "dataType": "string",
                            "type": "string",
                            "format": "",
                            "allowMultiple": false,
                            "required": true,
                            "minimum": 0,
                            "maximum": 0
                        },
                        {
                            "paramType": "query",
                            "name": "offset",
                            "description": "Offset",
                            "dataType": "int",
                            "type": "int",
                            "format": "",
                            "allowMultiple": false,
                            "required": true,
                            "minimum": 0,
                            "maximum": 0
                        },
                        {
                            "paramType": "query",
                            "name": "limit",
                            "description": "Offset",
                            "dataType": "int",
                            "type": "int",
                            "format": "",
                            "allowMultiple": false,
                            "required": true,
                            "minimum": 0,
                            "maximum": 0
                        }
                    ],
                    "responseMessages": [
                        {
                            "code": 200,
                            "message": "",
                            "responseType": "array",
                            "responseModel": "string"
                        },
                        {
                            "code": 400,
                            "message": "We need ID!!",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.APIError"
                        },
                        {
                            "code": 404,
                            "message": "Can not find ID",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.APIError"
                        }
                    ],
                    "produces": [
                        "application/json"
                    ]
                }
            ]
        },
        {
            "path": "/testapi/get-struct-array-by-string/{some_id}",
            "description": "get struct array by ID",
            "operations": [
                {
                    "httpMethod": "GET",
                    "nickname": "GetStructArrayByString",
                    "type": "array",
                    "items": {
                        "$ref": "git.corp.adobe.com.personal.test1.SimpleStructureWithAnnotations"
                    },
                    "summary": "get struct array by ID",
                    "parameters": [
                        {
                            "paramType": "path",
                            "name": "some_id",
                            "description": "Some ID",
                            "dataType": "string",
                            "type": "string",
                            "format": "",
                            "allowMultiple": false,
                            "required": true,
                            "minimum": 0,
                            "maximum": 0
                        },
                        {
                            "paramType": "query",
                            "name": "offset",
                            "description": "Offset",
                            "dataType": "int",
                            "type": "int",
                            "format": "",
                            "allowMultiple": false,
                            "required": true,
                            "minimum": 0,
                            "maximum": 0
                        },
                        {
                            "paramType": "query",
                            "name": "limit",
                            "description": "Offset",
                            "dataType": "int",
                            "type": "int",
                            "format": "",
                            "allowMultiple": false,
                            "required": true,
                            "minimum": 0,
                            "maximum": 0
                        }
                    ],
                    "responseMessages": [
                        {
                            "code": 200,
                            "message": "",
                            "responseType": "array",
                            "responseModel": "git.corp.adobe.com.personal.test1.SimpleStructureWithAnnotations"
                        },
                        {
                            "code": 400,
                            "message": "We need ID!!",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.APIError"
                        },
                        {
                            "code": 404,
                            "message": "Can not find ID",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.APIError"
                        }
                    ],
                    "produces": [
                        "application/json"
                    ]
                }
            ]
        },
        {
            "path": "/testapi/get-interface",
            "description": "get interface",
            "operations": [
                {
                    "httpMethod": "GET",
                    "nickname": "GetInterface",
                    "type": "git.corp.adobe.com.personal.test1.InterfaceType",
                    "items": {},
                    "summary": "get interface",
                    "responseMessages": [
                        {
                            "code": 200,
                            "message": "",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.InterfaceType"
                        },
                        {
                            "code": 400,
                            "message": "We need ID!!",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.APIError"
                        },
                        {
                            "code": 404,
                            "message": "Can not find ID",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.APIError"
                        }
                    ],
                    "produces": [
                        "application/json"
                    ]
                }
            ]
        },
        {
            "path": "/testapi/get-simple-aliased",
            "description": "get simple aliases",
            "operations": [
                {
                    "httpMethod": "GET",
                    "nickname": "GetSimpleAliased",
                    "type": "string",
                    "items": {},
                    "summary": "get simple aliases",
                    "responseMessages": [
                        {
                            "code": 200,
                            "message": "",
                            "responseType": "object",
                            "responseModel": "string"
                        },
                        {
                            "code": 400,
                            "message": "We need ID!!",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.APIError"
                        },
                        {
                            "code": 404,
                            "message": "Can not find ID",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.APIError"
                        }
                    ],
                    "produces": [
                        "application/json"
                    ]
                }
            ]
        },
        {
            "path": "/testapi/get-array-of-interfaces",
            "description": "get array of interfaces",
            "operations": [
                {
                    "httpMethod": "GET",
                    "nickname": "GetArrayOfInterfaces",
                    "type": "array",
                    "items": {
                        "$ref": "git.corp.adobe.com.personal.test1.InterfaceType"
                    },
                    "summary": "get array of interfaces",
                    "responseMessages": [
                        {
                            "code": 200,
                            "message": "",
                            "responseType": "array",
                            "responseModel": "git.corp.adobe.com.personal.test1.InterfaceType"
                        },
                        {
                            "code": 400,
                            "message": "We need ID!!",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.APIError"
                        },
                        {
                            "code": 404,
                            "message": "Can not find ID",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.APIError"
                        }
                    ],
                    "produces": [
                        "application/json"
                    ]
                }
            ]
        },
        {
            "path": "/testapi/get-struct3",
            "description": "get struct3",
            "operations": [
                {
                    "httpMethod": "GET",
                    "nickname": "GetStruct3",
                    "type": "git.corp.adobe.com.personal.test1.StructureWithSlice",
                    "items": {},
                    "summary": "get struct3",
                    "responseMessages": [
                        {
                            "code": 200,
                            "message": "",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.StructureWithSlice"
                        },
                        {
                            "code": 400,
                            "message": "We need ID!!",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.APIError"
                        },
                        {
                            "code": 404,
                            "message": "Can not find ID",
                            "responseType": "object",
                            "responseModel": "git.corp.adobe.com.personal.test1.APIError"
                        }
                    ],
                    "produces": [
                        "application/json"
                    ]
                }
            ]
        }
    ],
    "models": {
        "git.corp.adobe.com.personal.test1.APIError": {
            "id": "git.corp.adobe.com.personal.test1.APIError",
            "properties": {
                "ErrorCode": {
                    "type": "int",
                    "description": "",
                    "items": {},
                    "format": ""
                },
                "ErrorMessage": {
                    "type": "string",
                    "description": "",
                    "items": {},
                    "format": ""
                }
            }
        },
        "git.corp.adobe.com.personal.test1.InterfaceType": {
            "id": "git.corp.adobe.com.personal.test1.InterfaceType",
            "properties": null
        },
        "git.corp.adobe.com.personal.test1.SimpleStructureWithAnnotations": {
            "id": "git.corp.adobe.com.personal.test1.SimpleStructureWithAnnotations",
            "required": [
                "Name"
            ],
            "properties": {
                "Name": {
                    "type": "string",
                    "description": "",
                    "items": {},
                    "format": ""
                },
                "id": {
                    "type": "int",
                    "description": "",
                    "items": {},
                    "format": ""
                }
            }
        },
        "git.corp.adobe.com.personal.test1.StructureWithEmbededPointer": {
            "id": "git.corp.adobe.com.personal.test1.StructureWithEmbededPointer",
            "properties": {
                "Id": {
                    "type": "int",
                    "description": "",
                    "items": {},
                    "format": ""
                },
                "Name": {
                    "type": "array",
                    "description": "",
                    "items": {
                        "type": "byte"
                    },
                    "format": ""
                }
            }
        },
        "git.corp.adobe.com.personal.test1.StructureWithEmbededStructure": {
            "id": "git.corp.adobe.com.personal.test1.StructureWithEmbededStructure",
            "properties": {
                "Id": {
                    "type": "int",
                    "description": "",
                    "items": {},
                    "format": ""
                },
                "Name": {
                    "type": "array",
                    "description": "",
                    "items": {
                        "type": "byte"
                    },
                    "format": ""
                }
            }
        },
        "git.corp.adobe.com.personal.test1.StructureWithSlice": {
            "id": "git.corp.adobe.com.personal.test1.StructureWithSlice",
            "properties": {
                "Id": {
                    "type": "int",
                    "description": "",
                    "items": {},
                    "format": ""
                },
                "Name": {
                    "type": "array",
                    "description": "",
                    "items": {
                        "type": "byte"
                    },
                    "format": ""
                }
            }
        }
    }
}`,}

最佳答案

转到您拥有 docs.goweb.go 的目录,并假设您使用 下载了 swagger 包去获取 github.com/yvasiyarov/swagger,以下内容应该适合你:

$ go run web.go docs.go --staticPath=$GOPATH/src/github.com/yvasiyarov/swagger/swagger-ui --host=127.0.0.1 --port=3000

基本上,这里发生了几件事。如果你查看 web.go-example: https://github.com/yvasiyarov/swagger/blob/master/web.go-example#L14-L17 ,您可以看到它使用了一些默认值,这些默认值在您的本地环境中不起作用,因此您可以执行以下两项操作之一:

1 - 每次都运行上面显示的命令,所以你指定了 swagger-ui 路径,以及你所在的 hostport想为它服务。

2 - 修改 web.go,使其采用您的默认配置,您只需运行 go run web.go docs.go。基于上面的命令,更改将是这样的:

var port = flag.String("port", "3000", "Port")
var staticContent = flag.String("staticPath", "$GOPATH/src/github.com/yvasiyarov/swagger/swagger-ui", "Path to folder with Swagger UI")

然后转到http://127.0.0.1:3000/swagger-ui/玩得开心!

注意:如果您在其他地方有您的 swagger 源(不在您的 $GOPATH 中,请确保您使用该路径。即

1 - 在你的命令中:

$ go run ... -staticPath=/some/path/.../swagger/swagger-ui

2 - 或者在你的 web.go

var staticContent = flag.String("staticPath", "/some/path/.../swagger/swagger-ui", "Path to folder with Swagger UI")

关于Golang swagger 返回 404 not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40077813/

有关Golang swagger 返回 404 not found的更多相关文章

  1. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  2. ruby - 检查字符串是否包含散列中的任何键并返回它包含的键的值 - 2

    我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案

  3. ruby - Ruby 中的隐式返回值是怎么回事? - 2

    所以我开始关注ruby​​,很多东西看起来不错,但我对隐式return语句很反感。我理解默认情况下让所有内容返回self或nil但不是语句的最后一个值。对我来说,它看起来非常脆弱(尤其是)如果你正在使用一个不打算返回某些东西的方法(尤其是一个改变状态/破坏性方法的函数!),其他人可能最终依赖于一个返回对方法的目的并不重要,并且有很大的改变机会。隐式返回有什么意义?有没有办法让事情变得更简单?总是有返回以防止隐含返回被认为是好的做法吗?我是不是太担心这个了?附言当人们想要从方法中返回特定的东西时,他们是否经常使用隐式返回,这不是让你组中的其他人更容易破坏彼此的代码吗?当然,记录一切并给出

  4. ruby-on-rails - ruby 日期方程不返回预期的真值 - 2

    为什么以下不同?Time.now.end_of_day==Time.now.end_of_day-0.days#falseTime.now.end_of_day.to_s==Time.now.end_of_day-0.days.to_s#true 最佳答案 因为纳秒数不同:ruby-1.9.2-p180:014>(Time.now.end_of_day-0.days).nsec=>999999000ruby-1.9.2-p180:015>Time.now.end_of_day.nsec=>999999998

  5. ruby - 从 String#split 返回的零长度字符串 - 2

    在Ruby1.9.3(可能还有更早的版本,不确定)中,我试图弄清楚为什么Ruby的String#split方法会给我某些结果。我得到的结果似乎与我的预期相反。这是一个例子:"abcabc".split("b")#=>["a","ca","c"]"abcabc".split("a")#=>["","bc","bc"]"abcabc".split("c")#=>["ab","ab"]在这里,第一个示例返回的正是我所期望的。但在第二个示例中,我很困惑为什么#split返回零长度字符串作为返回数组的第一个值。这是什么原因呢?这是我所期望的:"abcabc".split("a")#=>["bc"

  6. ruby-on-rails - 为什么在安装 Ruby 1.9.3 时出现 404 错误? - 2

    我最近对我的计算机(OS-MacOSX10.6.8)进行了删除,并且我正在重新安装我所有的开发工具。我再次安装了RVM;但是,它不会让我安装Ruby1.9.3。到目前为止我已经尝试过:rvminstall1.9.3rvm安装1.9.3-p194rvm安装1.9.3-p448rvminstall1.9.3--with-gcc=clang所有返回相同的命令行错误:Searchingforbinaryrubies,thismighttakesometime.Nobinaryrubiesavailablefor:osx/10.6/x86_64/ruby-1.9.3-p448.Continuin

  7. ruby - 为什么 Integer.respond_to?( :even? ) 返回 false? - 2

    我一直在研究RubyKoans,我发现about_open_classes.rbkoan很有趣。特别是他们修改Integer#even?方法的最后一个测试。我想尝试一下这个概念,所以我打开了Irb并尝试运行Integer.respond_to?(:even?),但令我惊讶的是我得到了错误。然后我尝试了Fixnum.respond_to?(:even?)并得到了错误。我还尝试了Integer.respond_to?(:respond_to?)并得到了true,当我执行2.even?时,我也得到了true。我不知道发生了什么。谁能告诉我缺少什么? 最佳答案

  8. ruby - Time.to_i 是否总是以 UTC 返回自 EPOCH 以来的秒数? - 2

    无论时间在哪个时区表示,时区差异是否总是被忽略?直觉上,对于那些使用UTC+2的人来说,从EPOCH开始经过的秒数应该更高。然而,事实并非如此。 最佳答案 Epoch基于utc时区https://en.wikipedia.org/wiki/Unix_time它与您当前所在的时区无关。 关于ruby-Time.to_i是否总是以UTC返回自EPOCH以来的秒数?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.

  9. ruby-on-rails - Ruby 流量控制 : throw an exception, 返回 nil 还是让它失败? - 2

    我在思考流量控制的最佳实践。我应该走哪条路?1)不要检查任何东西并让程序失败(更清晰的代码,自然的错误消息):defself.fetch(feed_id)feed=Feed.find(feed_id)feed.fetchend2)通过返回nil静默失败(但是,“CleanCode”说,你永远不应该返回null):defself.fetch(feed_id)returnunlessfeed_idfeed=Feed.find(feed_id)returnunlessfeedfeed.fetchend3)抛出异常(因为不按id查找feed是异常的):defself.fetch(feed_id

  10. ruby-on-rails - 如何让 Rails View 返回其关联的操作名称? - 2

    我有一个非常简单的Controller来管理我的Rails应用程序中的静态页面:classPagesController我怎样才能让View模板返回它自己的名字,这样我就可以做这样的事情:#pricing.html.erb#-->"Pricing"感谢您的帮助。 最佳答案 4.3RoutingParametersTheparamshashwillalwayscontainthe:controllerand:actionkeys,butyoushouldusethemethodscontroller_nameandaction_nam

随机推荐