将Clojure与基于注释的REST服务器一起使用

Ral*_*lph 11 rest jax-rs clojure

我正在考虑使用Clojure编写REST服务器.

我有使用RESTEasy和Java的经验.它使用注释将URL,模板参数和查询参数与Java类,方法和方法参数相关联.我相信Jersey REST服务器也使用注释(因为它也基于JAX-RS).

是否可以在Clojure中使用这些框架?是否有正式的方法将注释与功能相关联?

Ral*_*lph 9

我在即将出版的书"Clojure Programming"中找到了答案,Chas Emerick,Brian Carper和Christophe Grand.

如果使用定义新类型deftype,则可以为新创建的类添加注释:

(ns my.resources
  (:import (javax.ws.rs Path PathParam Produces GET)))

(definterface PersonService
  (getPerson [^Integer id]))

(deftype ^{Path "/people/{id}"} PersonResource []
  PersonService
  (^{GET true                                                
     Produces ["text/plain"]}
    getPerson
    [this ^{PathParam "id"} id]           
    ; blah blah blah    
  ))
Run Code Online (Sandbox Code Playgroud)

我不确定这是否合适gen-class.我需要做实验.

  • 如果您已经有一个使用基于注释的Web框架的现有大型项目,并且您需要说服同事认为Clojure非常适合. (5认同)