2011-10-01 27 views
11

我正在考虑使用Clojure编写REST服务器。使用Clojure和基于注释的REST服务器

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

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

回答

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  
)) 

我不知道这是否会与gen-class工作。我需要试验。

+0

这是矫枉过正。我会用compojure做到这一点 – jorgeu

+4

如果你已经有了一个使用基于注解的web框架的大型项目,并且你需要让同事相信Clojure非常适合,那么不要这么做。 – Ralph

相关问题