Статическое содержимое в трикотаже Гризли
Я хочу обслуживать статические HTML-страницы в / и /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, "/");
Мой вопрос, где я должен положить статический файл в
2 ответа
В вашем случае статические данные должны быть расположены в /static
папка в корне вашего диска.
Есть несколько вариантов размещения статических ресурсов:
вы можете обслуживать статические ресурсы из вашей файловой системы по абсолютному пути (например,
/www/static/
):StaticHttpHandler staticHttpHandler = new StaticHttpHandler("/www/static/"); server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");
или встроил ваши ресурсы в архив jar (например,
/www/static.jar
):StaticHttpHandler staticHttpHandler = new CLStaticHttpHandler(new URLClassLoader(new URL[] {new URL("file:///www/static.jar")})); server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");
или вставьте статические ресурсы в jar вашего приложения:
StaticHttpHandler staticHttpHandler = new CLStaticHttpHandler(YourApp.class.getClassLoader()); server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");
ВНИМАНИЕ Невозможно преобразовать CLStaticHttpHandler в StaticHttpHandler!
CLStaticHttpHandler staticHttpHandler = new CLStaticHttpHandler(YourApp.class.getClassLoader());
server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");