【问题标题】:Dropwizard Jersey set URL pattern breaking Swagger 404?Dropwizard Jersey 设置打破 Swagger 404 的 URL 模式?
【发布时间】:2017-05-21 20:38:55
【问题描述】:

不久前我在我的 Dropwizard 服务中添加了 swagger,它运行良好。

但是现在我想添加一个简单的 html 页面并配置资源路径以提供来自 /assets 的内容,它破坏了招摇和 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() 方法调用时它会抱怨

多个servlet映射到路径/*: assets[mapped:JAVAX_API:null],io.dropwizard.jersey.setup.JerseyServletContainer-2e5b7fba[mapped:EMBEDDED:null]

但是,如果我删除资产包,Swagger 会再次正常工作吗?

我怎样才能让我的索引页和 swagger 在 Dropwizard/Jetty 上工作。

如果你能解释发生了什么,奖励积分!

【问题讨论】:

    标签: java jersey dropwizard


    【解决方案1】:

    您的应用程序或静态资产都可以从 根路径,但不是两者。后者在使用 Dropwizard 时很有用 返回一个 Javascript 应用程序。要启用它,请将您的应用程序移动到 一个子网址。

    来源:http://www.dropwizard.io/1.1.0/docs/manual/core.html#serving-assets

    bootstrap.addBundle(new AssetsBundle("/assets", "/", "index.html"));

    此行正在尝试将在 assets 资源目录中找到的资源挂载到 root 上下文路径。可能 Jersey 应用程序已经安装在那里。

    【讨论】:

      【解决方案2】:

      您可以在配置文件中添加 uriPrefix,如下所示

      swagger:
        title: Test Application
        resourcePackage: com.abc.xyz
        uriPrefix: /api/v1
      

      这将 /api/v1 作为基本 url

      【讨论】:

        猜你喜欢
        • 2016-11-17
        • 1970-01-01
        • 2016-07-31
        • 1970-01-01
        • 1970-01-01
        • 2018-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多