Sri*_*ini 1 clojure compojure compojure-api
我传递路径参数来从数据库中获取数据.
终点
http://localhost:3000/hello/1000
GET方法代码
码
(ns clojure-dauble-business-api.core
(:require [compojure.api.sweet :refer :all]
[ring.util.http-response :refer :all]
[clojure-dauble-business-api.logic :as logic]
[clojure.tools.logging :as log]
[clojure-dauble-business-api.domain.artwork])
(:import [clojure_dauble_business_api.domain.artwork Artwork]))
(defapi app
(GET ["/hello/:id", :id #"[0-9]+"] [id]
(log/info "Function begins from here" id)
(ok {:artwork (logic/artwork-id id)})))
Run Code Online (Sandbox Code Playgroud)
当我从邮递员到达终点时,我收到此错误,
org.postgresql.util.PSQLException: ERROR: operator does not exist: integer = character varying
Run Code Online (Sandbox Code Playgroud)
我已经知道值id作为String值传递给查询.
在传递给Query之前,将Path参数类型更改为Number的最佳方法是什么?
使用Compojure 1.4.0及更高版本,您可以使用语法强制参数
[id :<< as-int]
Run Code Online (Sandbox Code Playgroud)
另一种选择是使用Java互操作将id转换为Integer:
(Integer/parseInt id)
Run Code Online (Sandbox Code Playgroud)
由于您使用的是compojure-api,因此您还可以使用Schema或spec 强制输入和输出数据.