本文介绍了在同一个命名空间Clojure的DEFTYPE调用函数抛出" java.lang.IllegalStateException:试图调用未FN:"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把Clojure的成大量使用新泽西州和批注一个现有的Java项目。我希望能够利用现有的自定义注释,过滤器等的previous工作。到目前为止,我已经大致使用在的第9章发现javax.ws.rs注解DEFTYPE方法。

I'm placing Clojure into an existing Java project which heavily uses Jersey and Annotations. I'd like to be able to leverage the existing custom annotations, filters, etc of the previous work. So far I've been roughly using the deftype approach with javax.ws.rs annotations found in Chapter 9 of Clojure Programming.

(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专注于互操作,并连线到新泽西,并且应用程序逻辑是不同的(更喜欢的Clojure我想写)。

As you can see from the comments, I'd like to call functions outside the deftype, but within the same namespace. At least in my mind, this allows me to have the deftype focus on interop and wiring up to Jersey, and the application logic to be separate (and more like the Clojure I want to write).

然而,当我这样做,我得到了以下异常:

However when I do this I get the following exception:

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

有一些独特的东西有关DEFTYPE和命名空间?

Is there something unique about a deftype and namespaces?

推荐答案

...滑稽我对这个问题的时间并没有产生一个答案,直到后,我在这里问:)

... funny my hours on this problem did not yield an answer until after I asked here :)

它看起来像命名空间加载和deftypes在这一职务。正如我怀疑DEFTYPE不会自动加载的命名空间。由于在后发现,我能够通过添加需要这样来解决这个问题:

It looks like namespace loading and deftypes was addressed in this post. As I suspected the deftype does not automatically load the namespace. As found in the post, I was able to fix this by adding a require like this:

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

这篇关于在同一个命名空间Clojure的DEFTYPE调用函数抛出" java.lang.IllegalStateException:试图调用未FN:"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 11:10