Java – vertx Web: where do I put the Webroot folder?

It should be easy, but not so far I've been using vert X 2 and recently switched to vert x 3. I thought I would try a simple vertx web example, but no more than a simple static file

My server class contains the following code snippets:

HttpServer server = vertx.createHttpServer();
    Router router = ...;
    router.route("/static/*").handler(StaticHandler.create().setCachingEnabled(false));
    server.requestHandler(router::accept).listen(ctx.port);

I'm using eclipse, but I've been trying to run vertx. Com from the command line I'm also using Maven I have three Webroot folders, and vert X can find them:

myproject/webroot
myproject/src/main/resources/webroot
myproject/src/main/java/webroot

Each of these "webroots" contains an index HTML and a CSS / base CSS file

The first is in the root folder of my project The second is in the Maven resource folder, and the third should be flat on my classpath In my eclipse running configuration, I added myproject / SRC / main / resources / Webroot to the classpath, and I made sure my working directory was set to 'myproject' When running from the command line, I am in the myproject directory, and my script is as follows:

JAVA_OPTS="-Dmyproject.port=8099" CLASSPATH="src/main/java:src/main/resources:target/dependencies/*:target/classes" vertx run com.my.MyProject

Anyway, when I try any of the following URLs, I always get 404s:

http://localhost:8099
http://localhost:8099/
http://localhost:8099/index.html
http://localhost:8099/static/css/base.css

Is there anything else I need to do?

Solution

The solution I find depends on the answer to the question: where will the static content be at runtime and where do you want vertx to serve?

In my case, the installed file system will be the location where the static content is located (not in the jar file), and I want vertx to provide services from this location so that it can be updated in real time So, I set the JVM system property vertx Disablefilecpresolving is set to true to disable classpath resolution in statichandler

Then place the Webroot folder in the directory where you started the JVM In my case, the script I use ensures that the CWD of the JVM is always < app root > / bin, so it is sufficient to delete the content in < app root > / bin / Webroot If you can't guarantee where the JVM will start, it may be more stringent, because you may need to pass the absolute path to the statichandler pointing to this fixed location Webroot (), but I think there is an open question about support (see here)

It would be easier to package static content into a jar You can add "Webroot" to the jar as a resource and put all the interesting content there In this case, you do not want to disable classpath resolution, so set vertx Disablefilecpresolving is set to false or not Now when you run vertx, you will find Webroot in the jar file and extract its contents to < CWD > vertx / . File cache - < guid > (where CWD is the JVM from which to start) and provide content from there Please note that if you plan to update the content in real time, this is not feasible, because when vertx is closed, any changes to the files in this directory will be lost, because vertx web will delete the directory On reboot, it will retrieve the original file from the jar resource again and the changes will be lost

Anyway, I hope it helps

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>