使用Clojure和基于注释的REST服务器

时间:2023-01-10 22:52:09

I am considering writing a REST Server using Clojure.

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

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

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

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

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

1 个解决方案

#1


9  

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

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

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

如果使用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    
  ))

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

我不确定这是否适用于gen-class。我需要做实验。

#1


9  

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

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

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

如果使用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    
  ))

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

我不确定这是否适用于gen-class。我需要做实验。