设置URL模式会破坏Swagger 404吗? [英] Dropwizard Jersey set URL pattern breaking Swagger 404?
问题描述
不久前,我在Dropwizard服务中添加了摇摇欲坠的功能,效果很好.
I added swagger to my Dropwizard service a while back and it was working fine.
但是,现在我想添加一个简单的html页面并配置资源路径以提供/assets
中的内容,因此它已经断掉了招摇和api
带有404
响应
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
这是我的代码
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()方法调用时会抱怨
When I try and delete the two setUrlPattern() method calls it complains
多个servlet映射到路径/*:资产[mapped:JAVAX_API:null],io.dropwizard.jersey.setup.JerseyServletContainer-2e5b7fba [mapped:EMBEDDED:null]
Multiple servlets map to path /*: assets[mapped:JAVAX_API:null],io.dropwizard.jersey.setup.JerseyServletContainer-2e5b7fba[mapped:EMBEDDED:null]
但是,如果我删除资产捆绑包,那么Swagger可以再次正常工作吗?
However if I delete the assets bundle Swagger works ok again?
我如何才能同时获得索引页面和大张旗鼓地在Dropwizard/Jetty上工作.
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!
推荐答案
您的应用程序或静态资产都可以通过 根路径,但不能同时使用.后者在使用Dropwizard进行操作时非常有用 返回一个Javascript应用程序.要启用它,请将您的应用程序移至 子网址.
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应用程序已经安装在那里.
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.
这篇关于设置URL模式会破坏Swagger 404吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!