Pedestal框架开发指南:路由、拦截器与错误处理全解析
1. 创建路由
在Pedestal中,拦截器与定义客户端与应用交互端点的路由相关联。Pedestal提供了三种定义路由的方式:详细格式、表格格式和简洁格式。这三种格式都以Clojure数据结构定义,详细格式为Map,表格格式为Set,简洁格式为Vector。这三种格式只是为了方便使用,Pedestal内部会在处理之前将所有路由定义转换为详细格式,使用 expand-routes
函数完成转换。
以下是详细格式中定义路由的关键字及其描述:
| 关键字 | 示例值 | 描述 |
| ---- | ---- | ---- |
| :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-constraints | {:name #”.+” :search #