Last active
February 24, 2020 17:00
-
-
Save seltzer1717/f67e77bb5b47d08612fd7f636832a4ff to your computer and use it in GitHub Desktop.
Web Services with Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Example use of Java annotations in Clojure. | |
;; | |
;; The example uses javax.jws package to build a simle web service. | |
;; | |
;; Make sure you have Clojure and Java. | |
;; This example uses Clojure 1.8.0 and JDK8. | |
;; | |
;; Create the following folder structure: | |
;; | |
;; | webCalculator | |
;; | > src | |
;; | > com | |
;; | > example | |
;; | > service | |
;; | - calculator.clj | |
;; | > classes | |
;; | |
;; Navigate to 'webCalculator' folder and launch the REPL at Command Prompt: | |
;; "<path to jdk>/bin/java.exe" -cp "<path to clojure>/clojure-1.8.0.jar;src;classes;. clojure.main | |
;; | |
;; (I'm on Windows 10 - Do what you need to do for your OS.) | |
;; | |
;; At REPL type: | |
;; (load "com/example/service/calculator") | |
;; (compile 'com.example.service.calculator) | |
;; (def ep (com.example.service.calculator/endpoint)) | |
;; | |
;; Now we're going to create the client classes from our published web service. | |
;; "<path to jdk>/bin/wsimport.exe" -d classes -s src -keep -verbose http://localhost:1965/calculator?wsdl | |
;; | |
;; Finally run client from another REPL: | |
;; | |
;; (let [service (com.example.service.calculator.CalculatorTypeService.) | |
;; calculator (.getCalculatorTypePort service)] | |
;; (println "2+3: " (.add calculator 2 3)) | |
;; (println "4+7: " (.add calculator 4 7))) | |
;; | |
;; When done you should stop the endpoint in the first REPL: | |
;; (.stop ep) | |
(ns com.example.service.calculator | |
(:import | |
(javax.jws WebService WebMethod) | |
(javax.xml.ws Endpoint))) | |
(defprotocol Calculator | |
(add [this a b])) | |
(deftype | |
^{WebService {:targetNamespace "http://calculator.service.example.com"}} | |
CalculatorType [] | |
Calculator | |
(^{WebMethod []} add [this a b] (+ a b))) | |
(defn endpoint [] | |
(Endpoint/publish "http://localhost:1965/calculator" (CalculatorType.))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment