0

I have the following configuration in my Main.java file. I check if the ZKUI_BASEURL is set in env or in properties file and then accordingly set the contextPath.

When I do this, the GET requests for the following css and js files which are in src/main/resources/webapp/ return 404. All the controller requests are working fine its just the static content which is not loaded.

What I want to do is serve static content and rest apis on /org/zkui/ base path, so that in future we can run this behind a reverse proxy.

    String webFolder = "webapp";
    Server server = new Server();

    WebAppContext servletContextHandler = new WebAppContext();
    String serverBaseUrl;
    if( (serverBaseUrl = System.getenv("ZKUI_BASEURL")) == null) {
        if((serverBaseUrl = globalProps.getProperty("baseURL")) == null) {
            serverBaseUrl = "/";
        }
    }
    servletContextHandler.setContextPath(serverBaseUrl);
    servletContextHandler.setResourceBase("src/main/resources/" + webFolder);

    servletContextHandler.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*(/target/classes/|.*.jar)");
    servletContextHandler.setParentLoaderPriority(true);
    servletContextHandler.setInitParameter("useFileMappedBuffer", "false");
    servletContextHandler.setAttribute("globalProps", globalProps);

    ResourceHandler staticResourceHandler = new ResourceHandler();
    staticResourceHandler.setDirectoriesListed(false);
    Resource staticResources = Resource.newClassPathResource(webFolder);
    staticResourceHandler.setBaseResource(staticResources);
    staticResourceHandler.setWelcomeFiles(new String[]{"html/index.html"});

Essentially what should happen is - /org/zkui/home should be handled by a controller.

/org/zkui/images/image.png or org/zkui/js/bootstrap.min.js should be returned by a static resource handler from /webapp/images or webapp/js.

Niranjan
  • 517
  • 2
  • 4
  • 21
  • This question seems like a duplicate of https://stackoverflow.com/questions/20207477/serving-static-files-from-alternate-path-in-embedded-jetty/20223103 (already answered there) – Joakim Erdfelt Oct 27 '21 at 10:42

1 Answers1

0
servletContextHandler.setInitParameter("useFileMappedBuffer", "false")

If you value performance (and memory usage), don't do this. This configuration only exists for 1 purpose, developers on Microsoft Windows that are actively iterating and using hot-deploy features. Production should never have this configuration set.

Don't mix ResourceHandler, and DefaultServlet.

See past answers

As for serving alternate static content from a different location, you can use another DefaultServlet for that.

See past answers there too.

You mention REST APIs, make sure that you don't have your REST layer serving all of the static content (this is done with your REST configuration, and also your REST endpoint API url-pattern)

This has been answered before.

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136