Consuming WSDL in Clojure

≡放荡痞女 提交于 2019-12-21 04:38:09

问题


I need to consume a WSDL web service and the Java client-side code I've seen so far looks bloated and complicated. I was wondering whether a cleaner solution might exist in Clojure so that I may perhaps implement that part in Clojure and expose a simpler API to the Java code.


回答1:


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

It would create ./some.import.ns/*.class. So you can just use them in your clojure project

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

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



回答2:


Check out paos: https://github.com/xapix-io/paos

Lightweight and easy-to-use library to build SOAP clients from WSDL files.

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


来源:https://stackoverflow.com/questions/14407218/consuming-wsdl-in-clojure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!