【问题标题】:Clojure deftype calling function in the same namespace throws "java.lang.IllegalStateException: Attempting to call unbound fn:"同一命名空间中的 Clojure deftype 调用函数抛出“java.lang.IllegalStateException: Attempting to call unbound fn:”
【发布时间】:2012-06-08 17:46:27
【问题描述】:

我将 Clojure 放入一个大量使用 Jersey 和 Annotations 的现有 Java 项目中。我希望能够利用以前工作的现有自定义注释、过滤器等。到目前为止,我一直在大致使用 Clojure Programming 第 9 章中的带有 javax.ws.rs 注释的 deftype 方法。

(ns my.namespace.TestResource
  (:use [clojure.data.json :only (json-str)])
  (:import [javax.ws.rs DefaultValue QueryParam Path Produces GET]
           [javax.ws.rs.core Response]))

;;My function that I'd like to call from the resource.
(defn get-response [to]
  (.build
    (Response/ok
      (json-str {:hello to}))))

(definterface Test
  (getTest [^String to]))

(deftype ^{Path "/test"} TestResource [] Test
  (^{GET true
     Produces ["application/json"]}
  getTest
  [this ^{DefaultValue "" QueryParam "to"} to]
  ;Drop out of "interop" code as soon as possible
  (get-response to)))

从 cmets 中可以看出,我想在 deftype 之外调用函数,但在同一个命名空间内。至少在我看来,这让我可以将 deftype 的重点放在互操作上并连接到 Jersey,并且应用程序逻辑是独立的(更像是我想要编写的 Clojure)。

但是,当我这样做时,会出现以下异常:

java.lang.IllegalStateException: Attempting to call unbound fn: #'my.namespace.TestResource/get-response

deftype 和命名空间有什么独特之处吗?

【问题讨论】:

    标签: clojure annotations interop


    【解决方案1】:

    ...有趣的是,我在这个问题上花费的时间直到我在这里提问后才得到答案:)

    看起来命名空间加载和 deftypes 已在 this post. 中得到解决,因为我怀疑 deftype 不会自动加载命名空间。正如在帖子中发现的那样,我可以通过添加这样的要求来解决这个问题:

    (deftype ^{Path "/test"} TestResource [] Test
      (^{GET true
         Produces ["application/json"]}
        getTest
        [this ^{DefaultValue "" QueryParam "to"} to]
        ;Drop out of "interop" code as soon as possible
        (require 'my.namespace.TestResource) 
        (get-response to)))
    

    【讨论】:

      猜你喜欢
      • 2011-11-14
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 2011-01-19
      • 2016-06-04
      相关资源
      最近更新 更多