Question

I am learning to use Closure/Compojure and I am having problems on building a small web app.

I have the following routes defined on mywebapp/routes.clj

(defroutes app-routes
  (GET "/" [] (index-page))
  (GET "/about" [] (about-page))
  (GET "/bluebutton" [] (bluebutton-page))
  (GET "/bluebutton/patient" [] (patient-handler))
  (route/resources "/")
  (route/not-found "No page"))

And the one that is not working /bluebutton/patient, where I am expecting to have a JSON response with the following code:

(use '[ring.middleware.json :only [wrap-json-response]]
     '[ring.util.response :only [response]])

(defn patient-handler []
  (println "patient-handler")
  (wrap-json-response (response {:body {:foo "bar"}})))

For some reason I am getting a 404 response on my browser but I am checking on REPL output that I am executing the code of patient-handler, do you guys know If I am missing something?

Thanks in advance! And sorry for my weird english!

Was it helpful?

Solution

wrap-json-response takes a function as it's argument and returns a new function that when called will to the json wrapping. like so:

(defn patient-handler []
  (println "patient-handler")
  (middleware/wrap-json-response (fn [_] (response {:body {:foo "bar"}}))))

though a more normal app would have this split into it's own function (or entire namespace):

(ns hello-web.handler
  (:require [compojure.core :refer :all]
            [compojure.handler :as handler]
            [ring.middleware.json :as middleware]
            [compojure.route :as route]
            [ring.util.response :refer [response]]))

(defroutes app-routes
  (GET "/" [] "Hello World")
  (route/resources "/")
  (GET "/bluebutton/patient" [] (patient-handler))
  (route/not-found "Not Found"))

(def app
  (handler/site app-routes))

(defn create-json-response [request]
  (response {:body {:foo "bar"}}))

(defn patient-handler []
  (println "patient-handler")
  (middleware/wrap-json-response create-json-response))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top