Vertx web. Get resource from JAR's root

719 views
Skip to first unread message

Alexey Ivanov

unread,
Nov 5, 2016, 4:46:39 PM11/5/16
to vert.x
Hello,

I have a JAR file with index.html and several other HTML/JS files in its root. 

I want to serve this files using StaticHandler:

router.route("/static/*").handler(StaticHandler.create(""))

But requests like /static/ or /static/index.html return 404, because method io.vertx.core.impl.FileResolver#resolveFile returns null, because it is called with parameter "/index.html" and cl.getResource(fileName) at line 111 couldn't find such resource.

Is any way to fix this problem?

Laputa

unread,
Nov 6, 2016, 9:03:10 PM11/6/16
to vert.x
try to put the static resource inside the fatjar... i am facing the same problem when deploy it to openshift using diy.

Patrick Conway

unread,
Nov 7, 2016, 10:56:47 AM11/7/16
to vert.x
using maven?
put static files in resources/webroot

it is the default webroot

public interface StaticHandler extends Handler<RoutingContext> {
String DEFAULT_WEB_ROOT = "webroot";


remove the empty string from your create call
router.route("/static/*").handler(StaticHandler.create())

Alexey Ivanov

unread,
Nov 7, 2016, 6:27:40 PM11/7/16
to vert.x
Thank you for your answer, Patrick, but unfortunately I can't move these resources to any other directory into JAR, because this JAR is not mine, and I retrieve it from remote maven repository. Now I'm using maven-dependency-plugin to unpack the resources and put them into webroot directory in my JAR, but I'm sure there should be more proper way to serve resources from a root of a JAR.

Patrick Conway

unread,
Nov 7, 2016, 7:19:23 PM11/7/16
to ve...@googlegroups.com

I think you will need a custom static handler. which loads the static resources from the classpath then.
using getResource to find the file
http://stackoverflow.com/questions/1900154/classpath-resource-within-jar


On 7 Nov 2016 11:27 pm, "Alexey Ivanov" <aif...@gmail.com> wrote:
Thank you for your answer, Patrick, but unfortunately I can't move these resources to any other directory into JAR, because this JAR is not mine, and I retrieve it from remote maven repository. Now I'm using maven-dependency-plugin to unpack the resources and put them into webroot directory in my JAR, but I'm sure there should be more proper way to serve resources from a root of a JAR.

--
You received this message because you are subscribed to a topic in the Google Groups "vert.x" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vertx/j1AdluZ-Dnk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vertx+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/8ca2bdeb-63d2-45ce-955b-438708a49bea%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tim Fox

unread,
Nov 9, 2016, 5:07:40 AM11/9/16
to vert.x
You can set the webroot when creating the StaticHandler, but surely serving files from the top is a really bad idea... won't this mean the public will have access to all your class files and any other files in your application?
Reply all
Reply to author
Forward
0 new messages