␡
- Assumptions
- Benefits
- The Recipe—Code
- Testing the Solution
- Notes on the Recipe
- Conclusion
This chapter is from the book
The Recipe—Code
Create the project using Leiningen and the Compojure template:
lein new compojure rest-demo
Modify the project.clj to have the following contents:
(defproject rest-demo "0.1.0-SNAPSHOT" :min-lein-version "2.0.0" :dependencies [[org.clojure/clojure "1.7.0-beta2"] [compojure "1.3.4"] [ring/ring-defaults "0.1.5"]] :plugins [[lein-ring "0.9.5"]] :ring {:handler rest-demo.handler/app} :profiles {:dev {:dependencies [[javax.servlet/servlet-api "2.5"] [ring/ring-mock "0.2.0"]]}})
Ensure that the file rest-demo/src/rest_demo/handler.clj looks like this:
(ns rest-demo.handler (:require [compojure.core :refer :all] [compojure.route :as route] [ring.middleware.defaults :refer [wrap-defaults site-defaults]])) (defn handle-http [] (context "/:id" [id] (defroutes api-routes (GET "/" [] (str "get called: " id "\n")) (POST "/" {form-params :form-params} (str "post called: " id "\n" form-params " \n")) (PUT "/" req (str "put called with params: " req)) (DELETE "/" [] (str "delete called: " id "\n"))))) (defroutes app-routes (handle-http) (route/not-found (str "This is the default page - try " "<a href='http://localhost:4000/33'>this</a>\n"))) (def app (wrap-defaults app-routes (assoc-in site-defaults [:security :anti-forgery] false)))