Swagger UI configuration via jetty.xml

我是研究僧i 提交于 2019-12-11 17:35:15

问题


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.

My problem is the definition of the resourceBase property.

Here's the example configuration:

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);

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>

回答1:


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.



来源:https://stackoverflow.com/questions/48261369/swagger-ui-configuration-via-jetty-xml

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