clojure-spring-guide

Guidelines for using Clojure with Spring Framework

Clojure Style Guide

Controllers

and then return using

(json/generate-string {:a 1 :b 2})
(ns ...
  (:import (java.util.concurrent CompletableFuture)
           (java.util.function Supplier)))
           
(defmacro async
  "Wraps the given `body` inside a CompletableFuture."
  [& body]
  `(CompletableFuture/supplyAsync
     (reify
       Supplier
       ~(list 'get '[this] (cons 'do body)))))

and then use it like

(async
  (println "Async Controller method")
  (+ 3 5))

Complete Example

(ns com.clojurespring.controller
  (:require [cheshire.core :as json])
  (:import (org.springframework.web.bind.annotation RestController GetMapping PathVariable)))

(gen-class
  :name ^{RestController {}} com.clojurespring.SampleController
  :methods [[^{GetMapping {:value    ["/hello"]
                           :produces ["application/json;charset=UTF-8"}}
                           hello [^{PathVariable {:value "name"}} String] java.util.concurrent.Future]])

(defn -hello
  [this name]
  (async
    (println "Inside async controller...")
    (json/generate-string {:a       1
                           :message (str "Hello " name " from Clojure controller"))))

Request

curl -X GET -H "Accept: application/json;charset=UTF-8" "http://server.name/hello/rich"

Response

{
  "a": 1,
  "message": "Hello rich from Clojure controller"
}

Contributing

Nothing written in this guide is set in stone. It’s my desire to work together with everyone interested in Clojure coding style, so that we could ultimately create a resource that will be beneficial to the entire Clojure community.

Feel free to open tickets or send pull requests with improvements. Thanks in advance for your help!