问题
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