Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, 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?

like image 366
Ralph Avatar asked Oct 01 '11 18:10

Ralph


1 Answers

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.

like image 52
Ralph Avatar answered Oct 11 '22 01:10

Ralph