1

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.

madeinQuant
  • 1,721
  • 1
  • 18
  • 29
  • have you read the error message? In the original call you only pass two parameters, in the attempt that doesn't work, you pass 3 – Stultuske Jun 08 '23 at 07:12
  • @Stultuske, thank for you reply. I read the error messages, I am not familiar with Clojure, this is why I seek comment how to fix it – madeinQuant Jun 08 '23 at 07:19
  • 1
    `Javalin/get` looks like a call to a *static* method. Did you try `(.get app "/" (fn [ctx] (.result ctx "Hello World")))` instead? You could possibly chain the ops: `(-> (Javalin/create) (.get "/" (fn [ctx] (.result ctx "Hello World"))) (.start 7070))`. – Rulle Jun 08 '23 at 07:27

1 Answers1

4

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 get approximately translates to:

Javalin.get(app, "/", (ctx) -> ctx.result("Hello World"));

Just as you call result on ctx with (.result ctx "Hello World"), you need to call get the same way: (.get app "/" (fn ...)).

However, you will see that you are not getting the appropriate type for the handler function. Use reify to create an instance of Handler - you just have to add another argument to handle ("this"):

Dependency: [io.javalin/javalin "5.5.0"]

(ns ...
  (:import (io.javalin Javalin)
           (io.javalin.http Handler))
  (:gen-class))

(defn -main []
  (doto (Javalin/create)
    (.get "/" (reify Handler
                (handle [_ ctx] (.result ctx "Hello World"))))
    (.start 7070)))

You can also use proxy:

(defn -main []
  (doto (Javalin/create)
    (.get "/" (proxy [Handler] []
                (handle [ctx] (.result ctx "Hello World"))))
    (.start 7070)))
Martin Půda
  • 7,353
  • 2
  • 6
  • 13
  • 3
    `Handler` is an interface. Use `reify` for interfaces, and `proxy` only in dire need. Your way works, of course, but `reify` has been better since 2010. – amalloy Jun 08 '23 at 07:32
  • amalloy, martin thank you for your comment, Clojure web services; this is a whole new concept to me. I am reading "https://practical.li/clojure-web-services/" to figure out what it is. thank you for your help. – madeinQuant Jun 09 '23 at 03:36