【问题标题】:Gin Error panic: wildcard route conflicts with existing childrenGin 错误恐慌:通配符路由与现有子节点冲突
【发布时间】:2020-03-27 13:43:37
【问题描述】:

我使用https://github.com/gin-gonic/gin,我正在尝试实现这样的嵌套路由:

// SetupRouter initialize routes and handlers
func SetupRouter() *gin.Engine {
    r := gin.Default()

    r.GET("/", controllers.Welcome)

    r.GET("/resources", controllers.GetResources)   
    r.GET("/resources/otherroute", controllers.GetOResources)   
    r.GET("/resources/:id", controllers.GetResourcesByID)   
    r.GET("/resources/:id/sub-resources", GetSubResources)
    r.GET("/resources/:id/sub-resources/:srid", GetSubResourcesByID)
    r.GET("/resources/:id/sub-resources/:srid/ssub-resources", GetSSubResources)
    r.GET("/resources/:id/sub-resources/:srid/ssub-resources/:ssrid", GetSSubResourcesByID)

    // .... etc.

    return r
}

但我遇到了奇怪的错误:

[GIN-debug] GET    /                         --> github.com/badis/so-

gin-nested-routes/controllers.Welcome (3 handlers)
[GIN-debug] GET    /resources                --> github.com/badis/so-gin-nested-routes/controllers.GetResources (3 handlers)
[GIN-debug] GET    /resources/otherroute     --> github.com/badis/so-gin-nested-routes/controllers.GetOResources (3 handlers)
[GIN-debug] GET    /resources/:id            --> github.com/badis/so-gin-nested-routes/controllers.GetResourcesByID (3 handlers)
panic: wildcard route ':id' conflicts with existing children in path '/resources/:id'

goroutine 1 [running]:
github.com/gin-gonic/gin.(*node).insertChild(0xc0002516c0, 0xc0001edc01, 0xc0002325eb, 0x3, 0xc0002325e0, 0xe, 0xc000224300, 0x3, 0x3)
    /home/badis/go/src/github.com/gin-gonic/gin/tree.go:294 +0x807
github.com/gin-gonic/gin.(*node).addRoute(0xc0002516c0, 0xc0002325e0, 0xe, 0xc000224300, 0x3, 0x3)
    /home/badis/go/src/github.com/gin-gonic/gin/tree.go:255 +0x4ea

我无法在 SO 内部或外部找到令人满意的解决方案。

如果你想快速重现问题,请看这里:https://github.com/badis/so-question-gin-routes

【问题讨论】:

  • 我相信您不能将动态细分与静态细分混为一谈。您可以使用/resources/otherroute/resources/:id,但不能同时使用。

标签: rest go routing web-frameworks


【解决方案1】:

试试这个:

// SetupRouter initialize routes and handlers
func SetupRouter() *gin.Engine {
    r := gin.Default()

    r.GET("/", controllers.Welcome)

    r.GET("/resources", controllers.GetResources)   
    r.GET("/resources/otherroute", controllers.GetOResources)   
    resources := r.Group("/resources/:rid")
    {
        resources.GET("", controllers.GetResourcesByID)
        resources.GET("otherroute", controllers.GetOResources)
        resources.GET("sub-resources", controllers.GetSubResources)
        subResources := resources.GET("sub-resources/:srid")
        {
            subResources.GET("", GetSubResourcesByID)
            subResources.GET("/ssub-resources", GetSSubResources)
            subResources.GET("/ssub-resources/:ssrid", GetSSubResourcesByID)
        }
    }

    // .... etc.

    return r
}

【讨论】:

    【解决方案2】:

    Gin 无法区分 /resources/otherroute/resources/:id,因为 otherroute 可以解释为 :id,因此您有冲突。

    【讨论】:

    • 它可以在 express.js 中完美运行,但我可以看到 Gin 还没有它。它可以检查 /resources/otherroute 。如果不匹配,则将在:id 中找到的任何内容放入其中。只要它没有更多的部分,例如:/resources/:id/sub-resources
    • 是的,优先考虑路线是有意义的。但可能有原因导致它没有完成。无论哪种方式,也许接受答案或接近问题:-)
    猜你喜欢
    • 2019-03-21
    • 2016-07-21
    • 2020-10-24
    • 2020-07-28
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 2015-09-14
    • 2014-07-20
    相关资源
    最近更新 更多