Pedestal框架:路由、拦截器与错误处理全解析
1. 创建路由
在Pedestal中,拦截器与路由相关联,这些路由定义了客户端与应用程序交互的端点。Pedestal提供了三种定义路由的方式:详细格式、表格格式和简洁格式。这三种格式都以Clojure数据结构表示,详细格式为Map,表格格式为Set,简洁格式为Vector。这些格式仅为方便使用,Pedestal内部会使用 expand-routes
函数将所有路由定义转换为详细格式进行处理。
1.1 详细格式关键字说明
详细格式是包含定义路由关键字的Map列表,以下是一些关键字及其描述:
| 关键字 | 示例值 | 描述 |
| — | — | — |
| :route-name | :pedestal-play.service/about-page | 路由的唯一名称 |
| :app-name | :pedestal-play | 应用程序的可选名称 |
| :path | /about/:id | 路径URI |
| :method | :get | HTTP动词,如:get、:put、:post、:delete、:any等 |
| :scheme | :http | 可选,如:http和:https |
| :host | somehost.com | 可选的主机名 |
| :port | 8080 | 端口号 |
| :interceptors | 拦截器向量 | 包含:name、:enter、:leave和:error键的拦截器Map |
| :query-constrain