英文:
How to translate a simple web framework in Javalin into Clojure?
问题
Here's the translated code in 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)))
And here's the deps.edn
file:
{: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"}}}
The error you encountered, "No matching method get found taking 3 args for class io.javalin.Javalin," suggests that the Javalin/get
method in Clojure might not match the Java code structure perfectly. You may need to adjust the syntax or update the way you're using the Javalin/get
method to match the Clojure API for Javalin.
英文:
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.
答案1
得分: 4
你的代码存在的第一个问题是,你调用了Javalin类的一个不存在的静态方法。你对get
的调用大致翻译为:
Javalin.get(app, "/", (ctx) -> ctx.result("Hello World"));
就像你在ctx
上调用result
一样,你需要以相同的方式调用get
:(.get app "/" (fn ...))
。
然而,你会发现你没有得到处理函数的适当类型。使用reify
来创建Handler的实例 - 你只需要在handle
中添加另一个参数("this"):
依赖: [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)))
你也可以使用proxy
:
(defn -main []
(doto (Javalin/create)
(.get "/" (proxy [Handler] []
(handle [ctx] (.result ctx "Hello World"))))
(.start 7070)))
英文:
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)))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论