通过jetty.xml进行Swagger UI配置 [英] Swagger UI configuration via jetty.xml

查看:202
本文介绍了通过jetty.xml进行Swagger UI配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Swagger UI添加到在Jetty上运行的Java EE项目中.我有一个程序化的配置示例,但是由于我的服务器配置是在jetty.xml文件中定义的,因此我很难将其转换为XML.

I'm trying to add Swagger UI to a Java EE project running on Jetty. I have a programmatic configuration example but I'm struggling to convert that to XML since my server configuration is defined in the jetty.xml file.

我的问题是resourceBase属性的定义.

My problem is the definition of the resourceBase property.

这是示例配置:

ResourceHandler rh = new ResourceHandler();
rh.setResourceBase(InsectopediaServer.class.getClassLoader()
                .getResource("META-INF/resources/webjars/swagger-ui/2.1.4").toURI().toString());
ContextHandler resourceContext = new ContextHandler();
resourceContext.setContextPath("/api/");
resourceContext.setHandler(rh);

ContextHandlerCollection contexts = new ContextHandlerCollection();
contexts.setHandlers(new Handler[] { servletContext, resourceContext });

 server.setHandler(contexts);

这是我当前的 jetty.xml 的相关部分:

And here's the relevant section of my current jetty.xml:

<Set name="handler">
    <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
        <Set name="handlers">
            <Array type="org.eclipse.jetty.server.Handler">
                <Item>
                    <New class="org.eclipse.jetty.server.handler.ContextHandler">
                          <Set name="contextPath">/api</Set>
                          <Set name="handler">
                              <New class="org.eclipse.jetty.server.handler.ResourceHandler">
                                  <Set name="resourceBase"></Set>
                              </New>
                          </Set>
                    </New>
                  </Item>
                <Item>
                    <New id="Contexts"
                        class="org.eclipse.jetty.server.handler.ContextHandlerCollection" />
                </Item>
                <Item>
                    <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler" />
                </Item>
            </Array>
        </Set>
    </New>
</Set>

推荐答案

我不确定是否有更好的方法,但是我相信简单方法(虽然不是最灵活的)是复制swagger-ui文件进入webapp目录(最终进入"api"之类的文件夹).无需进一步配置,即可立即访问swagger-ui.

I'm not sure if there's a better way, but I believe that the simples approach, although not the most flexible, is to copy the swagger-ui files into the webapp directory (eventually to a folder such as "api"). This will make swagger-ui immediately accessible without further configuration.

这篇关于通过jetty.xml进行Swagger UI配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆