The main issue was in annotations of the code that I was using spring based annotations and using jakarta.ws.rs namespace. Here are my steps that solved the issue:
- 1. Upgraded the gradle as mentioned in the bottom
- 2. Fixed annotations
- 3. Configure jersey servlet and openapi servlet from swagger-core as mentioned below
- 4. Added content of `swagger-ui/dist/*` into `/src/resources/webapp` and pointed the index.html to `localhost:8080/openapi/swagger.json` where my openapi.yaml is being generated
Build.gradle:
plugins {
id 'java'
id 'application'
id "io.swagger.core.v3.swagger-gradle-plugin" version "2.1.9"
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
ext {
javaMainClass = "io.swagger.Main"
}
application {
mainClassName = javaMainClass
}
dependencies {
implementation platform('org.glassfish.jersey:jersey-bom:3.0.2')
implementation 'org.glassfish.jersey.containers:jersey-container-grizzly2-http'
implementation 'org.glassfish.jersey.inject:jersey-hk2'
implementation 'org.glassfish.jersey.media:jersey-media-json-binding'
implementation 'org.apache.commons:commons-lang3:3.7'
implementation 'io.swagger.core.v3:swagger-jaxrs2-jakarta:2.1.9'
implementation 'jakarta.ws.rs:jakarta.ws.rs-api:3.0.0'
implementation 'jakarta.servlet:jakarta.servlet-api:5.0.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}
resolve {
outputFileName = 'MyRestAPI'
outputFormat = 'JSON'
prettyPrint = 'TRUE'
classpath = sourceSets.main.runtimeClasspath
buildClasspath = classpath
resourcePackages = ['io.swagger']
outputDir = file('test')
}
Jersey & openapi servlet into Jetty Server:
// Setup Jetty Servlet
ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
contexts.addHandler(servletContextHandler);
// Setup API resources
ServletHolder jersey = servletContextHandler.addServlet(ServletContainer.class, "/api/*");
jersey.setInitOrder(1);
jersey.setInitParameter("jersey.config.server.provider.packages",
"com.cloudian.hfs.handlers;io.swagger.v3.jaxrs2.integration.resources");
// Expose API definition independently into yaml/json
ServletHolder openApi = servletContextHandler.addServlet(OpenApiServlet.class, "/openapi/*");
openApi.setInitOrder(2);
openApi.setInitParameter("openApi.configuration.resourcePackages",
"com.cloudian.hfs.handlers");
// Setup Swagger-UI static resources
String resourceBasePath = ServiceLoader.class.getResource("/webapp").toExternalForm();
servletContextHandler.setWelcomeFiles(new String[] {"index.html"});
servletContextHandler.setResourceBase(resourceBasePath);
servletContextHandler.addServlet(new ServletHolder(new DefaultServlet()), "/*");
And then I was able to access swagger.json at http://localhost:8080/