开发者

Using Clojure with an annotation-based REST Server

I am considering writing a REST Server using Clojure.

I have experience using RESTEasy with Java. It uses annotations to associate URLs, template parameters, and query parameters with Java classes, 开发者_JAVA技巧methods, and method parameters. I believe that the Jersey REST Server also uses annotations (since it, too, is based on JAX-RS).

Is it possible to use these frameworks with Clojure? Is there an official way to associate annotations with functions?


I found the answer in the forth-coming book "Clojure Programming", by Chas Emerick, Brian Carper, and Christophe Grand.

If you define a new type with deftype, you can add annotations the newly created class:

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

I'm not sure if this will work with gen-class. I'll need to experiment.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜