繁体   English   中英

同一命名空间中的Clojure deftype调用函数抛出“java.lang.IllegalStateException:Attempting to called unbound fn:”

[英]Clojure deftype calling function in the same namespace throws “java.lang.IllegalStateException: Attempting to call unbound fn:”

我将Clojure放入现有的Java项目中,该项目大量使用Jersey和Annotations。 我希望能够利用以前工作的现有自定义注释,过滤器等。 到目前为止,我已经大致使用了在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)))

从注释中可以看出,我想调用deftype之外的函数,但是在同一个命名空间内。 至少在我看来,这允许我将deftype专注于interop并连接到Jersey,并且应用程序逻辑是独立的(更像我想写的Clojure)。

但是,当我这样做时,我得到以下异常:

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

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

...搞笑我在这个问题上的时间直到我问到这里之后才得到答案:)

这篇文章中,它看起来像命名空间加载和deftypes 我怀疑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)))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM