Skip to content

Instantly share code, notes, and snippets.

@seltzer1717
Last active February 24, 2020 17:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seltzer1717/f67e77bb5b47d08612fd7f636832a4ff to your computer and use it in GitHub Desktop.
Save seltzer1717/f67e77bb5b47d08612fd7f636832a4ff to your computer and use it in GitHub Desktop.
Web Services with Clojure
;; 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