Beego为何永久出现404未知错误?
我将beego用于我的项目,但是在设置项目后,我只会遇到404个错误... 我的主机和路由器都可以。 任何问题的想法,我一直在寻找几个小时,徒劳的...
main.go
import (
_ "bealinkapi/routers"
"github.com/beego/beego/v2/client/orm"
beego "github.com/beego/beego/v2/server/web"
_ "github.com/lib/pq"
"time"
)
func main() {
orm.RegisterDriver("Postgres", orm.DRPostgres)
orm.DefaultTimeLoc = time.UTC
o, _ := beego.AppConfig.String("sqlconn")
err := orm.RegisterDataBase("MyAppName", "postgres", o)
if err != nil {
panic(err)
}
if beego.BConfig.RunMode == "dev" {
beego.BConfig.WebConfig.DirectoryIndex = true
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
}
beego.Run()
}
Router.Go
// @APIVersion 1.0.0
// @Title beego Test API
// @Description beego has a very cool tools to autogenerate documents for your API
// @Contact [email protected]
// @TermsOfServiceUrl http://beego.me/
// @License Apache 2.0
// @LicenseUrl http://www.apache.org/licenses/LICENSE-2.0.html
package routers
import (
"bealinkapi/controllers"
beego "github.com/beego/beego/v2/server/web"
)
func init() {
ns := beego.NewNamespace("/v1",
beego.NSNamespace("/bl_user",
beego.NSInclude(
&controllers.BlUserController{},
),
),
)
beego.AddNamespace(ns)
}
Console: 2022/04/11 15:04:55.627 [D] [Router.go:1272] | 127.0.0.1 | 404 | 265.248µs | nomatch | get/v1/bl_user/1
I use Beego for my project, but after setting up the project, I only get 404 errors...
My main.go is ok, as well as my router.
Any idea of the problem, I've been looking for several hours, in vain...
main.go
import (
_ "bealinkapi/routers"
"github.com/beego/beego/v2/client/orm"
beego "github.com/beego/beego/v2/server/web"
_ "github.com/lib/pq"
"time"
)
func main() {
orm.RegisterDriver("Postgres", orm.DRPostgres)
orm.DefaultTimeLoc = time.UTC
o, _ := beego.AppConfig.String("sqlconn")
err := orm.RegisterDataBase("MyAppName", "postgres", o)
if err != nil {
panic(err)
}
if beego.BConfig.RunMode == "dev" {
beego.BConfig.WebConfig.DirectoryIndex = true
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
}
beego.Run()
}
router.go
// @APIVersion 1.0.0
// @Title beego Test API
// @Description beego has a very cool tools to autogenerate documents for your API
// @Contact [email protected]
// @TermsOfServiceUrl http://beego.me/
// @License Apache 2.0
// @LicenseUrl http://www.apache.org/licenses/LICENSE-2.0.html
package routers
import (
"bealinkapi/controllers"
beego "github.com/beego/beego/v2/server/web"
)
func init() {
ns := beego.NewNamespace("/v1",
beego.NSNamespace("/bl_user",
beego.NSInclude(
&controllers.BlUserController{},
),
),
)
beego.AddNamespace(ns)
}
console:2022/04/11 15:04:55.627 [D] [router.go:1272] | 127.0.0.1| 404 | 265.248µs| nomatch| GET /v1/bl_user/1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在使用 swagger 接口测试 api 调用时,我遇到了类似的问题,但是当我进行 postman 调用时,这是正常的,由于某种原因,问题出在 swagger 上。此外,当我创建新的命名空间和路由器时,在邮递员中测试时一切都按预期工作。
I had a similar issue when using the swagger interface to test the api calls, but when I did a postman call it was normal, for some reason the problem was with swagger. Also when I created new namespaces and routers everything worked as expected when testing in postman.
我也有同样的问题。从Bee v2.0.2中,您必须使用额外的命令生成路由器。
bee v2.0.3发行说明
I had the same issue. From Bee V2.0.2 you have to generate the routers using an extra command.
Bee V2.0.3 release notes
Try this:
检查名为 commentrouter_controller.go 的文件
似乎您的项目中缺少此文件。
commentRouter_controller.go是自动生成的。如果没有,请检查此链接以获取更多信息 https://github.com/beego.com/beego/beego/beego/beego/beego/issues/issues/issues/issues /2520
Check the file named commentsRouter_controller.go
Seems this file is missing in your project.
commentsRouter_controller.go is generated automatically. If not please check this link for more info https://github.com/beego/beego/issues/2520