如何将Javalin中的简单Web框架翻译成Clojure?

huangapple go评论64阅读模式
英文:

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)))

huangapple
  • 本文由 发表于 2023年6月8日 15:11:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定