Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static content in Grizzly jersey

I want to serve static html pages in / and /index.html

final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);

BASE_URL = http://locahost:8080/api/

StaticHttpHandler staticHttpHandler = new StaticHttpHandler("/static");
server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");

My question is where should i put the static file in

like image 452
Rentonie Avatar asked Nov 19 '25 14:11

Rentonie


1 Answers

In your case, static data should be located in /static folder in root of your disk.

There are a couple of options where the static resources can be located:

  • you can serve static resources from your filesystem via absolute path (e.g. /www/static/):

    StaticHttpHandler staticHttpHandler = new StaticHttpHandler("/www/static/");
    server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");
    
  • or embedded your resources into jar archive (e.g. /www/static.jar):

    StaticHttpHandler staticHttpHandler = new CLStaticHttpHandler(new URLClassLoader(new URL[] {new URL("file:///www/static.jar")}));
    server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");
    
  • or embed static resources inside your application jar:

    StaticHttpHandler staticHttpHandler = new CLStaticHttpHandler(YourApp.class.getClassLoader());
    server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");
    
like image 156
Maciej Lach Avatar answered Nov 22 '25 04:11

Maciej Lach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!