'Sparkjava static files 404 after restart
I'm using spark java for a small web server. In my main class, I setup the stuff I need:
public static void startServer()
{
if (!starting && !stopping)
{
starting = true;
// Initialize server
port(port);
secure(keystoreLocation, keystorePass, null, null);
// Register all routes
initRoutes();
// Start server
init();
starting = false;
}
}
And in initRoutes, I have:
public static void initRoutes()
{
Spark.staticFileLocation("/public");
}
When I run that code, I am able to navigate to localhost and see the contents of my index.html page, which is in src/main/resources/public. I even see this in the log:
10:24:17.151 [INFO ] - StaticResourceHandler configured with folder = /public
But, when I run a process to restart the server, the staticFileLocation seems to be lost.
Here is my stopServer method and restartServer method.
public static void stopServer()
{
if (!stopping && !starting)
{
stopping = true;
stop();
try
{
// This is temporary until the ability to wait for the service to
// stop comes along
Thread.sleep(10 * 1000);
}
catch (InterruptedException ex)
{
LogUtil.warn("Failed to wait while server shuts down", ex);
}
stopping = false;
}
}
public static void restartServer()
{
stopServer();
startServer();
}
The log shows this line:
10:33:52.768 [WARN ] - Static file location has already been set
But when I try to access the home page again, I get a 404 and the log says this:
10:34:22.260 [INFO ] - The requested route [/] has not been mapped in Spark for Accept: [text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8]
My other routes (omitted for brevity) re-initialize just fine. I can restart the server and still navigate to them with no problem.
I don't need the ability to change the static file location, but I would really like for that setting to persist between restarts of the server. Maybe it's the way I'm doing the restart? I couldn't find much information on a proper process for doing so.
Solution 1:[1]
Upgrade to a newer version of Spark-Java i.e. 2.7.0. This issues has been resolved: https://github.com/perwendel/spark/pull/902
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ranil Wijeyratne |
