【问题标题】:How to map multiple HTTP Verbs to the same path in HTTP4K如何将多个 HTTP 动词映射到 HTTP4K 中的同一路径
【发布时间】:2019-06-13 14:52:48
【问题描述】:

我有一个类似于以下在 HTTP4K 中运行良好的路由。然而,不得不重复调用 "/" bind 很烦人。我一直在寻找一种更简单的方式来表达 DSL,但似乎没有其他方法。有什么方法可以实现吗?

routes(
    "/things" bind routes(
        "/" bind Method.GET to allThings,
        "/{id:.*}" bind routes (
            "/" bind Method.GET to singleThing,
            "/" bind Method.DELETE to deleteThing,
            "/" bind Method.PUT to addOrUpdateThing
        )
    )
).asServer(Netty(8080))
    .start()

【问题讨论】:

    标签: rest kotlin http4k


    【解决方案1】:

    有一个同名的便捷函数接受Pair<Method, HttpHandler> 的可变参数,您应该可以删除前导"/" bind,如下所示:

    routes(
        "/things" bind routes(
            "/" bind Method.GET to allThings,
            "/{id:.*}" bind routes(
                Method.GET to singleThing,
                Method.DELETE to deleteThing,
                Method.PUT to addOrUpdateThing
            )
        )
    ).asServer(Netty(8080))
        .start()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      相关资源
      最近更新 更多