不久前,我将 swagger 添加到了我的 Dropwizard 服务中,并且运行良好。
但是现在我想添加一个简单的 html 页面并配置资源路径来提供来自 /assets
的内容,它已经破坏了 swagger 和 api
带有 404
响应
这是我的代码
public class NumericodeApplication extends Application<NumericodeConfiguration> {
public static void main(final String[] args) throws Exception {
new NumericodeApplication().run(args);
}
@Override
public String getName() {
return "Numericode";
}
@Override
public void initialize(final Bootstrap<NumericodeConfiguration> bootstrap) {
bootstrap.addBundle(new SwaggerBundle<NumericodeConfiguration>() {
@Override
protected SwaggerBundleConfiguration getSwaggerBundleConfiguration(NumericodeConfiguration configuration) {
return configuration.swaggerBundleConfiguration;
}
});
bootstrap.addBundle(new AssetsBundle("/assets", "/", "index.html")); //added this
}
@Override
public void run(final NumericodeConfiguration configuration, final Environment environment) {
environment.jersey().register(new Controller());
environment.jersey().setUrlPattern("/swagger"); //tried this
environment.jersey().setUrlPattern("/api/*"); //added this
}
}
当我尝试删除两个 setUrlPattern() 方法调用时,它会提示
Multiple servlets map to path /*: assets[mapped:JAVAX_API:null],io.dropwizard.jersey.setup.JerseyServletContainer-2e5b7fba[mapped:EMBEDDED:null]
但是,如果我删除资源包 Swagger 还能再次正常工作吗?
如何让我的索引页和 swagger 在 Dropwizard/Jetty 上工作。
如果你能解释发生了什么,就加分!
最佳答案
Either your application or your static assets can be served from the root path, but not both. The latter is useful when using Dropwizard to back a Javascript application. To enable it, move your application to a sub-URL.
来源:http://www.dropwizard.io/1.1.0/docs/manual/core.html#serving-assets
bootstrap.addBundle(new AssetsBundle("/assets", "/", "index.html"));
此行尝试挂载在 root
上下文路径的 assets
资源目录中找到的资源。也许 Jersey 应用程序已经安装在那里。
关于java - Dropwizard Jersey 设置 URL 模式打破 Swagger 404?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44101560/