Dropwizard Jersey set URL pattern breaking Swagger 404?

旧街凉风 提交于 2019-12-12 04:08:25

问题


I added swagger to my Dropwizard service a while back and it was working fine.

However now that I am wanting to add a simple html page and configure the resource path to serve content from /assets it has broken swagger and the api with 404 responses

This is my code

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
    }
}

When I try and delete the two setUrlPattern() method calls it complains

Multiple servlets map to path /*: assets[mapped:JAVAX_API:null],io.dropwizard.jersey.setup.JerseyServletContainer-2e5b7fba[mapped:EMBEDDED:null]

However if I delete the assets bundle Swagger works ok again?

How can I get both my index page and swagger to work on Dropwizard/ Jetty.

Bonus points if you can explain what is going on!


回答1:


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.

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

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

This line is attempting to mount the resources found in assets resource directory at the root context path. Probably Jersey application is mounted there already.




回答2:


You can add uriPrefix in the config file like below

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

This will take /api/v1 as the base url



来源:https://stackoverflow.com/questions/44101560/dropwizard-jersey-set-url-pattern-breaking-swagger-404

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!