I have the following method:
package com.streambright.http.handlers;
import org.rapidoid.http.Req;
import org.rapidoid.u.U;
import java.util.Map;
public class EchoHandler {
public static Map<String, String> handleEcho(Req req) {
return U.map(req.headers());
}
}
When I use it from have it is perfectly fine:
On.get("/echo").managed(false).json(EchoHandler::handleEcho);
However when I try to do the same in Clojure it fails with wrong number of arguments.
Handler:
(ns s.echo-http-handler
(:import
[org.rapidoid.http Req ]
[org.rapidoid.u U ] )
(:gen-class
:methods [^:static [handler [org.rapidoid.http.Req] java.util.Map]]))
(defn -handler
[Req req]
(U/map (.headers req)))
When I start the server:
(ns s.http
(:require
[s.echo-http-handler :as echo ] )
(:import
[org.rapidoid.config Conf ]
[org.rapidoid.setup On ] ) )
(defn start
[]
(set-http-params!)
(.json (.managed (On/get "/echo") false) echo/-handler))
It starts up and throws the following error:
clojure.lang.ArityException: Wrong number of args (0) passed to: echo-http-handler/-handler
How do I pass in the req to the function in Clojure?
Assuming the problem is with how it's handeling Clojure functions, the following should work:
This function takes a Clojure function, and uses it to create an object that implements
ReqHandler. I think the problem was it was assuming (for some reason) that you wanted the 0-arity overload of.json, when you really wanted the overload that passes an argument to the callback. This helper should prevent that confusion.Then use as:
Although that could also be written as:
Or even with the handler inlined to show that it's still possible using a wrapper function to use terse anonymous function syntax:
Which is arguably a lot more readable. Because
thisis always implicitly the first argument when using Java interop,dotoand->are great at getting rid of nesting.