将 Clojure 与基于注释的 REST 服务器结合使用

发布于 2024-12-07 07:40:12 字数 318 浏览 5 评论 0原文

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

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

是否可以将这些框架与 Clojure 一起使用?有没有官方的方法将注释与函数关联起来?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

糖果控 2024-12-14 07:40:12

我在 Chas Emerick、Brian Carper 和 Christophe Grand 即将出版的《Clojure 编程》一书中找到了答案。

如果您使用 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。我需要进行实验。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文