在Clojure中使用WSDL

时间:2013-01-18 20:38:08

标签: clojure wsdl

我需要使用WSDL Web服务,到目前为止我看到的Java客户端代码看起来很臃肿和复杂。我想知道Clojure中是否存在更清晰的解决方案,以便我可以在Clojure中实现该部分并向Java代码公开更简单的API。

2 个答案:

答案 0 :(得分:9)

cd your_project_dir/src
wsimport -p some.import.ns http://.../service?wsdl

它会创建./some.import.ns/*.class。所以你可以在你的clojure项目中use

(ns your.ns ...
  (:import [some.import.ns some_WS_Service ...]))

(let [port (-> (some_WS_Service.) 
               .getSome_WS_ServicePort]
  (... (.someMethod port) ...))

答案 1 :(得分:0)

结帐:https://github.com/xapix-io/paos

  

轻巧易用的库可从WSDL文件构建SOAP客户端。

(require '[clj-http.client :as client])
(require '[paos.service :as service])
(require '[paos.wsdl :as wsdl])

(defn parse-response [{:keys [status body] :as response} body-parser fail-parser]
  (assoc response
         :body
         (case status
           200 (body-parser body)
           500 (fail-parser body))))

(let [soap-service   (wsdl/parse "http://www.thomas-bayer.com/axis2/services/BLZService?wsdl")
      srv            (get-in soap-service ["BLZServiceSOAP11Binding" :operations "getBank"])
      soap-url       (get-in soap-service ["BLZServiceSOAP11Binding" :url])
      soap-headers   (service/soap-headers srv)
      content-type   (service/content-type srv)
      mapping        (service/request-mapping srv)
      context        (assoc-in mapping ["Envelope" "Body" "getBank" "blz" :__value] "28350000")
      body           (service/wrap-body srv context)
      resp-parser    (partial service/parse-response srv)
      fault-parser   (partial service/parse-fault srv)]
  (-> soap-url
      (client/post {:content-type content-type
                    :body         body
                    :headers      (merge {} soap-headers)
                    :do-not-throw true})
      (parse-response resp-parser fault-parser)))