无法使用 vertx-web 从类路径中提供静态资源

huangapple go评论62阅读模式
英文:

Unable to serve static resources from classpath using vertx-web

问题

vertx-web文档中提到可以从类路径中提供静态资源,我在论坛/帮助主题中阅读的所有内容似乎都证实了这一点。不幸的是,我似乎无法使其工作。

public class MainVerticle extends AbstractVerticle {

  @Override
  public void start() {
    final Router router = Router.router(vertx);

    router.get("/greeting").handler(req -> req.response().end("Hello Vert.x!"));

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

    vertx.createHttpServer()
        .requestHandler(router)
        .listen(8080);
  }

}
@ExtendWith(VertxExtension.class)
class MainVerticleTest {

  @BeforeEach
  void prepare(Vertx vertx, VertxTestContext testContext) {
    vertx.deployVerticle(MainVerticle.class.getCanonicalName(), testContext.succeeding(id -> testContext.completeNow()));
  }

  @Test
  @DisplayName("Check that the server returns greeting")
  void checkServerHasStarted(Vertx vertx, VertxTestContext testContext) {
    WebClient webClient = WebClient.create(vertx);
    webClient.get(8080, "localhost", "/greeting")
      .as(BodyCodec.string())
      .send(testContext.succeeding(response -> testContext.verify(() -> {
        assertEquals(200, response.statusCode());
        assertTrue(response.body().length() > 0);
        assertTrue(response.body().contains("Hello Vert.x!"));
        testContext.completeNow();
      })));
  }

  @Test
  @DisplayName("Check that the server returns router-root.txt")
  void checkServerHasRootResource(Vertx vertx, VertxTestContext testContext) {
    WebClient webClient = WebClient.create(vertx);
    webClient.get(8080, "localhost", "/router-root.txt")
      .as(BodyCodec.string())
      .send(testContext.succeeding(response -> testContext.verify(() -> {
        assertEquals(200, response.statusCode());
        testContext.completeNow();
      })));
  }

    
  @Test
  @DisplayName("Check that the server returns router-package.txt")
  void checkServerHasPackageResource(Vertx vertx, VertxTestContext testContext) {
    WebClient webClient = WebClient.create(vertx);
    webClient.get(8080, "localhost", "/router-package.txt")
      .as(BodyCodec.string())
      .send(testContext.succeeding(response -> testContext.verify(() -> {
        assertEquals(200, response.statusCode());
        testContext.completeNow();
      })));
  }
}

checkServerHasStarted 测试通过,但其他两个测试失败,返回 404。这两个资源文件在这里存在:

  • src/main/resources/router-root.txt
  • src/main/resources/io/vertx/starter/router-package.txt

我已确认这些文件在其相对路径的fat jar中存在。

理想情况下,我希望从另一个包(Java项目)中包含这些资源,但首要任务是解决当前问题。

我是否需要在我的路由中做些什么,以使任一失败的测试通过?我刚开始使用Vert.x,所以我认为我的设置可能有问题。

完整项目:https://github.com/drkstr101/vertx-static-example

谢谢您提前的任何指导!

编辑

我意识到有一种接受 ClassLoaderStaticHandler.create 形式,这很可能是正确的做法。但是根据我所阅读的,这不应该是必需的,而且还有一个关于该功能将在下一个版本中被移除的警告。我是否可以假设,一旦该功能被弃用,从类路径中提供资源将完全正常工作?

英文:

The vertx-web documentation says that it is possible to serve static resources from the classpath, and everything I've read in forum/help threads seems to confirm this. Unfortunately I can't seem to get it to work.

public class MainVerticle extends AbstractVerticle {

  @Override
  public void start() {
    final Router router = Router.router(vertx);

    router.get("/greeting").handler(req -> req.response().end("Hello Vert.x!"));

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

    vertx.createHttpServer()
        .requestHandler(router)
        .listen(8080);
  }

}
@ExtendWith(VertxExtension.class)
class MainVerticleTest {

  @BeforeEach
  void prepare(Vertx vertx, VertxTestContext testContext) {
    vertx.deployVerticle(MainVerticle.class.getCanonicalName(), testContext.succeeding(id -> testContext.completeNow()));
  }

  @Test
  @DisplayName("Check that the server returns greeting")
  void checkServerHasStarted(Vertx vertx, VertxTestContext testContext) {
    WebClient webClient = WebClient.create(vertx);
    webClient.get(8080, "localhost", "/greeting")
      .as(BodyCodec.string())
      .send(testContext.succeeding(response -> testContext.verify(() -> {
        assertEquals(200, response.statusCode());
        assertTrue(response.body().length() > 0);
        assertTrue(response.body().contains("Hello Vert.x!"));
        testContext.completeNow();
      })));
  }

  @Test
  @DisplayName("Check that the server returns router-root.txt")
  void checkServerHasRootResource(Vertx vertx, VertxTestContext testContext) {
    WebClient webClient = WebClient.create(vertx);
    webClient.get(8080, "localhost", "/router-root.txt")
      .as(BodyCodec.string())
      .send(testContext.succeeding(response -> testContext.verify(() -> {
        assertEquals(200, response.statusCode());
        testContext.completeNow();
      })));
  }

    
  @Test
  @DisplayName("Check that the server returns router-package.txt")
  void checkServerHasPackageResource(Vertx vertx, VertxTestContext testContext) {
    WebClient webClient = WebClient.create(vertx);
    webClient.get(8080, "localhost", "/router-package.txt")
      .as(BodyCodec.string())
      .send(testContext.succeeding(response -> testContext.verify(() -> {
        assertEquals(200, response.statusCode());
        testContext.completeNow();
      })));
  }
}

checkServerHasStarted passes, but the other two are failing with 404. The two resource files exist here:

  • src/main/resources/router-root.txt
  • src/main/resources/io/vertx/starter/router-package.txt

And I have confirmed these files exist in the fat jar at their relative paths.

Ideally I would like to include these resources from another bundle (java project), but first things first.

Is there something I need to do with my routes to get either one of the failed tests to pass? I am just getting started with Vert.x, so I assume my setup is stupid somewhere.

Full Project: https://github.com/drkstr101/vertx-static-example

Cheers, and thanks in advance for any guidance!

EDIT

I realize there is a form of StaticHandler.create that accepts a ClassLoader, and is most likely how to go about this. However from what I read this should not be necessary, and also there is a deprecating warning about the feature being removed in the next version. May I assume that that serving resources from the classpath will be fully working once the feature is deprecated?

答案1

得分: 2

默认的资源文件夹是 webroot

在您的项目中创建一个 src/main/resources/webroot 文件夹,并将所有静态内容移动到其中。
或者配置 StaticHandler 来更改根文件夹。

附注:对于一个捕获所有路由,您可以省略路径。

router.route().handler(StaticHandler.create());
英文:

The default folder for resources is webroot.

Create a src/main/resources/webroot folder in your project and move all the static content to it.
Or configure the StaticHandler to change the root folder.

Side note: for a catch-all route you can omit the path

router.route().handler(StaticHandler.create());

huangapple
  • 本文由 发表于 2020年9月22日 08:38:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64001575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定