Beginner help with pedestal interceptors

So I’m trying to learn how to build an REST API with pedestal but I’m struggling with a simple json interceptor.

This is my code:

(ns weather-playlist.service
  (:require [io.pedestal.http :as http]))

(defn response [status body & {:as headers}]
  {:status status
   :body body
   :headers headers})

(def ok        (partial response 200))
(def created   (partial response 201))

(defn respond-hello [_req]
  (ok {:message "Hello, World!"}))

(def routes #{["/greet" :get respond-hello :route-name :greet]})

(def service-map (-> {::http/routes routes
                      ::http/type   :immutant
                      ::http/port   4000}
                     (http/default-interceptors)
                     (update ::http/interceptors into [http/json-body http/json-response])))

(println service-map)

(defn start []
  (http/start (http/create-server service-map)))

(defonce server (atom nil))

(defn start-dev []
  (reset! server
          (http/start (http/create-server
                        (assoc service-map
                               ::http/join? false)))))

(defn stop-dev []
  (http/stop @server))

(defn restart []
  (stop-dev)
  (start-dev))

Then I start my server and try to curl the /greet route and I receive this giant error:

[org.projectodd.wunderboss.web.Web] (nREPL-session-18ef0c73-47f8-4f7f-9cdb-93f8018597a6) Registered web context /
13:37:23.976 ERROR [io.pedestal.http.impl.servlet-interceptor] (XNIO-2 task-1) {:msg "Servlet code threw an exception", :throwable #error {
 :cause "Assert failed: (every? interceptor/interceptor? interceptors)"
 :via
 [{:type java.lang.AssertionError
   :message "Assert failed: (every? interceptor/interceptor? interceptors)"
   :at [io.pedestal.interceptor.chain$enqueue invokeStatic "chain.clj" 273]}]
 :trace
 [[io.pedestal.interceptor.chain$enqueue invokeStatic "chain.clj" 273]
  [io.pedestal.interceptor.chain$enqueue invoke "chain.clj" 273]
  [io.pedestal.interceptor.chain$execute invokeStatic "chain.clj" 389]
  [io.pedestal.interceptor.chain$execute invoke "chain.clj" 352]
  [io.pedestal.http.impl.servlet_interceptor$interceptor_service_fn$fn__19818invoke "servlet_interceptor.clj" 351]
  [io.pedestal.http.servlet.FnServlet service "servlet.clj" 28]
  [io.undertow.servlet.handlers.ServletHandler handleRequest "ServletHandler.java" 85]
  [io.undertow.servlet.handlers.FilterHandler$FilterChainImpl doFilter "FilterHandler.java" 129]
  [org.projectodd.wunderboss.web.async.websocket.WebSocketHelpyHelpertonFilter doFilter "WebSocketHelpyHelpertonFilter.java" 80]
  [io.undertow.servlet.core.ManagedFilter doFilter "ManagedFilter.java" 61]
  [io.undertow.servlet.handlers.FilterHandler$FilterChainImpl doFilter "FilterHandler.java" 131]
  [io.undertow.servlet.handlers.FilterHandler handleRequest "FilterHandler.java" 84]
  [io.undertow.servlet.handlers.security.ServletSecurityRoleHandler handleRequest "ServletSecurityRoleHandler.java" 62]
  [io.undertow.servlet.handlers.ServletDispatchingHandler handleRequest "ServletDispatchingHandler.java" 36]
  [io.undertow.servlet.handlers.security.SSLInformationAssociationHandler handleRequest "SSLInformationAssociationHandler.java" 131]
  [io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler handleRequest "ServletAuthenticationCallHandler.java" 57]
  [io.undertow.server.handlers.PredicateHandler handleRequest "PredicateHandler.java" 43]
  [io.undertow.security.handlers.AbstractConfidentialityHandler handleRequest"AbstractConfidentialityHandler.java" 46]
  [io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler handleRequest "ServletConfidentialityConstraintHandler.java" 64]
  [io.undertow.security.handlers.AuthenticationMechanismsHandler handleRequest "AuthenticationMechanismsHandler.java" 60]
  [io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler handleRequest "CachedAuthenticatedSessionHandler.java" 77]
  [io.undertow.security.handlers.AbstractSecurityContextAssociationHandler handleRequest "AbstractSecurityContextAssociationHandler.java" 43]
  [io.undertow.server.handlers.PredicateHandler handleRequest "PredicateHandler.java" 43]
  [io.undertow.server.handlers.PredicateHandler handleRequest "PredicateHandler.java" 43]
  [io.undertow.servlet.handlers.ServletInitialHandler handleFirstRequest "ServletInitialHandler.java" 292]
  [io.undertow.servlet.handlers.ServletInitialHandler access$100 "ServletInitialHandler.java" 81]
  [io.undertow.servlet.handlers.ServletInitialHandler$2 call "ServletInitialHandler.java" 138]
  [io.undertow.servlet.handlers.ServletInitialHandler$2 call "ServletInitialHandler.java" 135]
  [io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1 call "ServletRequestContextThreadSetupAction.java" 48]
  [io.undertow.servlet.core.ContextClassLoaderSetupAction$1 call "ContextClassLoaderSetupAction.java" 43]
  [io.undertow.servlet.handlers.ServletInitialHandler dispatchRequest "ServletInitialHandler.java" 272]
  [io.undertow.servlet.handlers.ServletInitialHandler handleRequest "ServletInitialHandler.java" 197]
  [io.undertow.server.handlers.HttpContinueReadHandler handleRequest "HttpContinueReadHandler.java" 65]
  [io.undertow.server.session.SessionAttachmentHandler handleRequest "SessionAttachmentHandler.java" 68]
  [io.undertow.server.Connectors executeRootHandler "Connectors.java" 211]
  [io.undertow.server.HttpServerExchange$1 run "HttpServerExchange.java" 809]
  [java.util.concurrent.ThreadPoolExecutor runWorker "ThreadPoolExecutor.java" 1144]
  [java.util.concurrent.ThreadPoolExecutor$Worker run "ThreadPoolExecutor.java" 642]
  [java.lang.Thread run "Thread.java" 1589]]}, :cause-trace "java.lang.AssertionError: Assert failed: (every? interceptor/interceptor? interceptors)\n at io.pedestal.interceptor.chain$enqueue.invokeStatic (chain.clj:273)\n    io.pedestal.interceptor.chain$enqueue.invoke (chain.clj:273)\n    io.pedestal.interceptor.chain$execute.invokeStatic (chain.clj:389)\n    io.pedestal.interceptor.chain$execute.invoke (chain.clj:352)\n    io.pedestal.http.impl.servlet_interceptor$interceptor_service_fn$fn__19818.invoke (servlet_interceptor.clj:351)\n io.pedestal.http.servlet.FnServlet.service (servlet.clj:28)\n    io.undertow.servlet.handlers.ServletHandler.handleRequest (ServletHandler.java:85)\n    io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter (FilterHandler.java:129)\n    org.projectodd.wunderboss.web.async.websocket.WebSocketHelpyHelpertonFilter.doFilter (WebSocketHelpyHelpertonFilter.java:80)\n    io.undertow.servlet.core.ManagedFilter.doFilter (ManagedFilter.java:61)\n    io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter (FilterHandler.java:131)\n    io.undertow.servlet.handlers.FilterHandler.handleRequest (FilterHandler.java:84)\n    io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest (ServletSecurityRoleHandler.java:62)\n    io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest (ServletDispatchingHandler.java:36)\n    io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest (SSLInformationAssociationHandler.java:131)\n    io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest (ServletAuthenticationCallHandler.java:57)\n    io.undertow.server.handlers.PredicateHandler.handleRequest (PredicateHandler.java:43)\n    io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest (AbstractConfidentialityHandler.java:46)\n    io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest (ServletConfidentialityConstraintHandler.java:64)\n    io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest (AuthenticationMechanismsHandler.java:60)\n    io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest (CachedAuthenticatedSessionHandler.java:77)\n    io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest (AbstractSecurityContextAssociationHandler.java:43)\n    io.undertow.server.handlers.PredicateHandler.handleRequest (PredicateHandler.java:43)\n    io.undertow.server.handlers.PredicateHandler.handleRequest (PredicateHandler.java:43)\n    io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest (ServletInitialHandler.java:292)\n    io.undertow.servlet.handlers.ServletInitialHandler.access$100 (ServletInitialHandler.java:81)\n    io.undertow.servlet.handlers.ServletInitialHandler$2.call (ServletInitialHandler.java:138)\n    io.undertow.servlet.handlers.ServletInitialHandler$2.call (ServletInitialHandler.java:135)\n    io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call (ServletRequestContextThreadSetupAction.java:48)\n    io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call (ContextClassLoaderSetupAction.java:43)\n    io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest (ServletInitialHandler.java:272)\n    io.undertow.servlet.handlers.ServletInitialHandler.handleRequest (ServletInitialHandler.java:197)\n    io.undertow.server.handlers.HttpContinueReadHandler.handleRequest (HttpContinueReadHandler.java:65)\n    io.undertow.server.session.SessionAttachmentHandler.handleRequest (SessionAttachmentHandler.java:68)\n    io.undertow.server.Connectors.executeRootHandler (Connectors.java:211)\n    io.undertow.server.HttpServerExchange$1.run (HttpServerExchange.java:809)\n    java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1144)\n    java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:642)\n    java.lang.Thread.run (Thread.java:1589)\n", :line358}

What that even means? I need to write JSON interceptor by hand?

Welcome! :wave:

If you look at this, it tells you that the registered interceptors contain things that are not valid interceptors. Without knowing exactly how Pedestal works, to me this looks like this line is wrong:

Maybe the http/json-body and http/json-response are not interceptors? Is there an interceptors namespace in Pedestal?

json-response is not an interceptor, just a utility function. I think you just need json-body - it sets the content-type to json and convert the datastructure in :body to json.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.