clojurenamespacesmiddlewareleiningenring

What is wrong with my namespace declaration in this clojure project?


I am using Clojure and leinigen to build a backend project.

This is a simplified version of my file:

(ns simplified...
  (:require [ring.middleware.json :refer [wrap-json-response]]
            [ring.util.response :refer [response]]
            [ring.middleware.json :refer [wrap-json-body]]
            [ring.middleware.defaults :refer [wrap-defaults]]
            [ring.adapter.jetty :as jetty]))

(defroutes rest-api-routes
  (POST "/api" req
        
        (clojure.pprint/pprint req)
        "hello"))

(defn import-atb
  [request-id tenant-id]
  (let [trial-balance-report  (middleware/wrap-json-body rest-api-routes {:keywords? true})])
  trial-balance-report)

After doing a lein clean, when I run lein repl, I get:

#error {
 :cause No such namespace: middleware

What is wrong with my file?


Solution

  • The quick fix is to substitute middleware/wrap-json-body for wrap-json-body because it's been referred into the namespace.

    require can take an :refer option. The :refer option, which you're using here, allows you to refer to that namespace's var directly from your own. The :as option will allow you to alias a namespace within your own. For example, you could (:require [ring.middleware.json :as ring-json]) and then refer to ring-json/wrap-json-body in your namespace.