![](/img/trans.png)
[英]http.ServeFile returns 404 not found error on the server golang
[英]Golang swagger returns 404 not found
我使用示例項目按照github頁面https://github.com/yvasiyarov/swagger上的說明進行操作,並且api文檔生成得很好。 但是,當我運行最后一個命令以運行並顯示API頁面時,我總是在那里找不到404
該web.go文件實質上是https://github.com/yvasiyarov/swagger/blob/master/web.go-example 。 它基本上是倉庫中的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.go
和web.go
的目錄,並假設您使用go get github.com/yvasiyarov/swagger
下載了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
路徑以及要為其提供服務的host
和port
。
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")
值得一提的是,它還支持代碼生成的逆過程。 您可以在此處找到其文檔: https : //goswagger.io/generate/spec.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.