There is a simple web framework in Javalin
import io.javalin.Javalin;
public class HelloWorld {
public static void main(String[] args) {
var app = Javalin.create(/*config*/)
.get("/", ctx -> ctx.result("Hello World"))
.start(7070);
}
}
Translated into Clojure.
(ns javalin.javalin101)
(import 'io.javalin.Javalin)
(defn -main []
(let [app (Javalin/create)]
(Javalin/get app "/" (fn [ctx] (.result ctx "Hello World")))
(.start app 7070)))
deps.edn
{:paths ["src"]
:deps
{org.clojure/clojure {:mvn/version "1.11.1"}
org.clojure/core.async {:mvn/version "1.6.673"}
org.clojure/data.json {:mvn/version "2.4.0"}
clj-time/clj-time {:mvn/version "0.15.2"}
;; java
io.javalin/javalin {:mvn/version "5.5.0"}}}
When I execute with calve-repl, an error encounter
; Syntax error (IllegalArgumentException) compiling . at (src/javalin/javalin101.clj:7:5).
; No matching method get found taking 3 args for class io.javalin.Javalin
Please feel free to comment how to fix it.
The first issue with your code, is that you are making a call to a non-existent static method of the Javalin class. Your call to
getapproximately translates to:Just as you call
resultonctxwith(.result ctx "Hello World"), you need to callgetthe same way:(.get app "/" (fn ...)).However, you will see that you are not getting the appropriate type for the handler function. Use
reifyto create an instance of Handler - you just have to add another argument tohandle("this"):You can also use
proxy: