forked from Qortal/qortal
ADDED: Swagger UI for API documentation
This commit is contained in:
parent
23b8fcc96e
commit
a075705885
81
pom.xml
81
pom.xml
@ -4,6 +4,9 @@
|
|||||||
<groupId>org.qora</groupId>
|
<groupId>org.qora</groupId>
|
||||||
<artifactId>qora-core</artifactId>
|
<artifactId>qora-core</artifactId>
|
||||||
<version>2.0.0-SNAPSHOT</version>
|
<version>2.0.0-SNAPSHOT</version>
|
||||||
|
<properties>
|
||||||
|
<swagger-ui.version>3.19.0</swagger-ui.version>
|
||||||
|
</properties>
|
||||||
<build>
|
<build>
|
||||||
<sourceDirectory>src</sourceDirectory>
|
<sourceDirectory>src</sourceDirectory>
|
||||||
<plugins>
|
<plugins>
|
||||||
@ -15,6 +18,74 @@
|
|||||||
<target>1.8</target>
|
<target>1.8</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<!-- unpack swagger-ui to target folder -->
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-dependency-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>swagger ui</id>
|
||||||
|
<phase>generate-resources</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>unpack</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<artifactItems>
|
||||||
|
<artifactItem>
|
||||||
|
<groupId>org.webjars</groupId>
|
||||||
|
<artifactId>swagger-ui</artifactId>
|
||||||
|
<version>${swagger-ui.version}</version>
|
||||||
|
</artifactItem>
|
||||||
|
</artifactItems>
|
||||||
|
<outputDirectory>${project.build.directory}/swagger-ui.unpacked</outputDirectory>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<!-- inject correct url to swagger json file into swwagger-ui -->
|
||||||
|
<plugin>
|
||||||
|
<groupId>com.google.code.maven-replacer-plugin</groupId>
|
||||||
|
<artifactId>replacer</artifactId>
|
||||||
|
<version>1.5.3</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>generate-resources</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>replace</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<file>${project.build.directory}/swagger-ui.unpacked/META-INF/resources/webjars/swagger-ui/${swagger-ui.version}/index.html</file>
|
||||||
|
<replacements>
|
||||||
|
<replacement>
|
||||||
|
<token>https://petstore.swagger.io/v2/swagger.json</token>
|
||||||
|
<value>/openapi.json</value>
|
||||||
|
</replacement>
|
||||||
|
</replacements>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<!-- add swagger-ui as resource to output package -->
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-resources-plugin</artifactId>
|
||||||
|
<version>3.1.0</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>copy-resources</id>
|
||||||
|
<phase>generate-resources</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>copy-resources</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<outputDirectory>${project.build.directory}/classes/resources/swagger-ui</outputDirectory>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>${project.build.directory}/swagger-ui.unpacked/META-INF/resources/webjars/swagger-ui/${swagger-ui.version}</directory>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
<repositories>
|
<repositories>
|
||||||
@ -138,5 +209,15 @@
|
|||||||
<artifactId>mail</artifactId>
|
<artifactId>mail</artifactId>
|
||||||
<version>1.5.0-b01</version>
|
<version>1.5.0-b01</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.webjars</groupId>
|
||||||
|
<artifactId>swagger-ui</artifactId>
|
||||||
|
<version>${swagger-ui.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-rewrite</artifactId>
|
||||||
|
<version>9.4.11.v20180605</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
@ -1,11 +1,15 @@
|
|||||||
package api;
|
package api;
|
||||||
|
|
||||||
import io.swagger.v3.jaxrs2.integration.resources.OpenApiResource;
|
import io.swagger.v3.jaxrs2.integration.resources.OpenApiResource;
|
||||||
|
import java.io.File;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import org.eclipse.jetty.rewrite.handler.RedirectPatternRule;
|
||||||
|
import org.eclipse.jetty.rewrite.handler.RewriteHandler;
|
||||||
|
|
||||||
import org.eclipse.jetty.server.Server;
|
import org.eclipse.jetty.server.Server;
|
||||||
import org.eclipse.jetty.server.handler.InetAccessHandler;
|
import org.eclipse.jetty.server.handler.InetAccessHandler;
|
||||||
|
import org.eclipse.jetty.servlet.DefaultServlet;
|
||||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||||
import org.eclipse.jetty.servlet.ServletHolder;
|
import org.eclipse.jetty.servlet.ServletHolder;
|
||||||
import org.glassfish.jersey.server.ResourceConfig;
|
import org.glassfish.jersey.server.ResourceConfig;
|
||||||
@ -36,16 +40,30 @@ public class ApiService {
|
|||||||
}
|
}
|
||||||
this.server.setHandler(accessHandler);
|
this.server.setHandler(accessHandler);
|
||||||
|
|
||||||
|
// url rewriting
|
||||||
|
RewriteHandler rewriteHandler = new RewriteHandler();
|
||||||
|
accessHandler.setHandler(rewriteHandler);
|
||||||
|
|
||||||
// context
|
// context
|
||||||
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
|
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
|
||||||
context.setContextPath("/");
|
context.setContextPath("/");
|
||||||
accessHandler.setHandler(context);
|
rewriteHandler.setHandler(context);
|
||||||
|
|
||||||
// API servlet
|
// API servlet
|
||||||
ServletContainer container = new ServletContainer(config);
|
ServletContainer container = new ServletContainer(config);
|
||||||
ServletHolder apiServlet = new ServletHolder(container);
|
ServletHolder apiServlet = new ServletHolder(container);
|
||||||
apiServlet.setInitOrder(1);
|
apiServlet.setInitOrder(1);
|
||||||
context.addServlet(apiServlet, "/*");
|
context.addServlet(apiServlet, "/*");
|
||||||
|
|
||||||
|
// Swagger-UI static content
|
||||||
|
ClassLoader loader = this.getClass().getClassLoader();
|
||||||
|
File swaggerUIResourceLocation = new File(loader.getResource("resources/swagger-ui/").getFile());
|
||||||
|
ServletHolder swaggerUIServlet = new ServletHolder("static-swagger-ui", DefaultServlet.class);
|
||||||
|
swaggerUIServlet.setInitParameter("resourceBase", swaggerUIResourceLocation.getAbsolutePath());
|
||||||
|
swaggerUIServlet.setInitParameter("dirAllowed","true");
|
||||||
|
swaggerUIServlet.setInitParameter("pathInfoOnly","true");
|
||||||
|
context.addServlet(swaggerUIServlet,"/api-documentation/*");
|
||||||
|
rewriteHandler.addRule(new RedirectPatternRule("/api-documentation", "/api-documentation/index.html")); // redirect to swagger ui start page
|
||||||
}
|
}
|
||||||
|
|
||||||
//XXX: replace singleton pattern by dependency injection?
|
//XXX: replace singleton pattern by dependency injection?
|
||||||
|
Loading…
x
Reference in New Issue
Block a user