我遵循了这个 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.go
和 web.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
路径,以及你所在的 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")
关于Golang swagger 返回 404 not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40077813/