如何使用 dropwizard 为每个 url 提供多个静态文件?
How to serve multiple static files using dropwizard for each url?
我用过,
bootstrap.addBundle(new AssetsBundle("/assets", "/", "a.js"));
bootstrap.addBundle(new MultiPartBundle());
但我需要提供一个文件,在输入 /b 时说 b.js,在输入 /c 时说 c.js。
如何操作?
您可以通过将 resourcePath 指定为实际文件来映射特定资源。因此,使用您提到的示例,您可以实现这一点:
bootstrap.addBundle(new AssetsBundle("/assets/a.js", "/a", null, "a"));
bootstrap.addBundle(new AssetsBundle("/assets/b.js", "/b", null, "b"));
bootstrap.addBundle(new AssetsBundle("/assets/c.js", "/c", null, "c"));
但是,我认为这不是一个好的做法,并且随着时间的推移会变得难以管理,以保持每个资源 URL 的映射。
更新
对于更易于维护的映射列表,我建议使用类似 Configurable Assets Bundle for Dropwizard, where multiple mappings can be described on the configuration file. I have created a simple DW project 的东西,它使用这个库并复制问题中描述的行为。
我用过,
bootstrap.addBundle(new AssetsBundle("/assets", "/", "a.js"));
bootstrap.addBundle(new MultiPartBundle());
但我需要提供一个文件,在输入 /b 时说 b.js,在输入 /c 时说 c.js。
如何操作?
您可以通过将 resourcePath 指定为实际文件来映射特定资源。因此,使用您提到的示例,您可以实现这一点:
bootstrap.addBundle(new AssetsBundle("/assets/a.js", "/a", null, "a"));
bootstrap.addBundle(new AssetsBundle("/assets/b.js", "/b", null, "b"));
bootstrap.addBundle(new AssetsBundle("/assets/c.js", "/c", null, "c"));
但是,我认为这不是一个好的做法,并且随着时间的推移会变得难以管理,以保持每个资源 URL 的映射。
更新 对于更易于维护的映射列表,我建议使用类似 Configurable Assets Bundle for Dropwizard, where multiple mappings can be described on the configuration file. I have created a simple DW project 的东西,它使用这个库并复制问题中描述的行为。