Ralph
Ralph

Reputation: 32264

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?

Upvotes: 12

Views: 1608

Answers (1)

Ralph
Ralph

Reputation: 32264

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.

Upvotes: 10

Related Questions