我正在练习Clojure教程“基本Web开发”http://clojure-doc.org/articles/tutorials/basic_web_development.html
最后,我遇到了一个问题。当我执行
$ java -jar target/clojure-webapp-0.1.0-SNAPSHOT-standalone.jar 8080
发生错误
Exception in thread "main" java.io.FileNotFoundException: 8080 (No such file or directory)
因此,端口号被解析为无法找到的文件的文件名。
我不熟悉Java,但我认为情况很清楚。端口号是典型的函数参数,而不是文件名。请解释一下,告诉我如何解决它。更多细节:
s@lokal:~/Dropbox/clojure-webapp$ lein uberjar
Created /home/s/Dropbox/clojure-webapp/target/clojure-webapp-0.1.0-SNAPSHOT.jar
Created /home/s/Dropbox/clojure-webapp/target/clojure-webapp-0.1.0-SNAPSHOT-standalone.jar
s@lokal:~/Dropbox/clojure-webapp$ cat ~/Dropbox/clojure-webapp/src/clojure_webapp/handler.clj
(ns clojure-webapp.handler
(:require [clojure-webapp.views :as views]
[compojure.core :refer :all]
[compojure.route :as route]
[ring.adapter.jetty :as jetty]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]])
(:gen-class))
(defroutes app-routes ; replace the generated app-routes with this
(GET "/"
[]
(views/home-page))
(GET "/add-location"
[]
(views/add-location-page))
(POST "/add-location"
{params :params}
(views/add-location-results-page params))
(GET "/location/:loc-id"
[loc-id]
(views/location-page loc-id))
(GET "/all-locations"
[]
(views/all-locations-page))
(route/resources "/")
(route/not-found "Not Found"))
(def app
(wrap-defaults app-routes site-defaults))
(defn -main
[& [port]]
(let [port (Integer. (or port
(System/getenv "PORT")
5000))]
(jetty/run-jetty #'app {:port port
:join? false})))
s@lokal:~/Dropbox/clojure-webapp$ java -jar target/clojure-webapp-0.1.0-SNAPSHOT-standalone.jar 8080
Exception in thread "main" java.io.FileNotFoundException: 8080 (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
at clojure.lang.Compiler.loadFile(Compiler.java:7449)
at clojure.main$load_script.invokeStatic(main.clj:278)
at clojure.main$script_opt.invokeStatic(main.clj:338)
at clojure.main$script_opt.invoke(main.clj:333)
at clojure.main$main.invokeStatic(main.clj:424)
at clojure.main$main.doInvoke(main.clj:387)
at clojure.lang.RestFn.applyTo(RestFn.java:137)
at clojure.lang.Var.applyTo(Var.java:702)
at clojure.main.main(main.java:37)
s@lokal:~/Dropbox/clojure-webapp$
我相信这个教程有点旧。恕我直言,你可能会更乐意使用Pedestal而不是Ring。你可以在这里找到一个“Hello World”教程:
我有一些工作代码是本教程的结果,您可以从这里克隆:
[email protected]:cloojure/pedestal-tutorial.git
在终端窗口中,通过以下方式启动代码:
> lein run
然后导航浏览器:
http://localhost:8890/echo ; demo/test route
http://localhost:8890/greet ; generic greeting
http://localhost:8890/greet?name=Bob ; normal greeting
http://localhost:8890/greet?name=Voldemort ; unmentionable!
如果你想坚持Ring / Compojure,另一个选择是Clojure Cookbook,它有例子。它可以打印(推荐)或在线提供:
这是另一本很棒的书: