I am very new to swagger world and getting confused about how to go around configuring openapi.yaml from my apis. I am doing a CODE FIRST approach where we document the existing APIs using swagger. Since my server is Jetty 11, it can not work with javax.servlet
, javax.*
and swagger.jersey2.jaxrs
dependencies.
After following the guidelines from Swagger 2.X Integration and Configuration and following the configuration for hooking swagger with jetty from Swagger Setup for Embedded Jetty Server, I used the following approach -
package com.example.hfs;
import com.example.hfs.*
import jakarta.servlet.Servlet;
import jakarta.servlet.ServletContainerInitializer;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
*
*/
public class HFS {
static public Properties props;
// Constants
final static public String RESPONSE_CONTENT_TYPE_JSON = "application/json;charset=UTF-8";
//final static public String RESPONSE_CONTENT_TYPE_TEXT = "text/html;charset=UTF-8";
public static void main(String[] args) throws Exception {
System.out.println("StartHFS");
initProperties();
// Create and configure a ThreadPool.
QueuedThreadPool threadPool = new QueuedThreadPool();
threadPool.setName("server");
// Create a Server instance.
Server server = new Server(threadPool);
// HTTP configuration and connection factory.
HttpConfiguration httpConfig = new HttpConfiguration();
HttpConnectionFactory http11 = new HttpConnectionFactory(httpConfig);
// Create a ServerConnector to accept connections from clients.
ServerConnector connector = new ServerConnector(server, 1, 1, http11);
connector.setPort(8080);
connector.setHost("0.0.0.0");
connector.setAcceptQueueSize(128);
server.addConnector(connector);
// Swagger Setup for Embedded Jetty Server
ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
server.setHandler(servletContextHandler);
// Setup API resources
ServletHolder apiServlet = servletContextHandler.addServlet(Servlet.class, "/api/*");
apiServlet.setInitOrder(1);
apiServlet.setInitParameter("com.sun.jersey.config.property.packages", "com.api.resources;io.swagger.jakarta.json;io.swagger.jakarta.listing");
// Setup Swagger Servlet
ServletHolder swaggerServlet = servletContextHandler.addServlet("DefaultJakartaConfig.class", "/swagger-core");
swaggerServlet.setInitOrder(2);
swaggerServlet.setInitParameter("api.version", "1.0.0");
addHandlers(server);
// Start the Server so it starts accepting connections from clients.
server.start();
server.join();
System.out.println("StartHFS DONE");
}
And my gradle contains following dependencies :
plugins {
id 'java'
// id 'io.swagger.core.v3.swagger-gradle-plugin' version '2.1.9'
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
compile 'org.eclipse.jetty:jetty-server:11.0.0'
compile 'org.eclipse.jetty:jetty-util:11.0.0'
compile group: 'org.json', name: 'json', version: '20201115'
compile group: 'org.eclipse.jetty', name: 'jetty-servlet', version: '11.0.0'
compile group: 'io.swagger.core.v3', name: 'swagger-core-jakarta', version: '2.1.9'
implementation 'io.swagger.core.v3:swagger-jaxrs2-jakarta:2.1.9'
implementation 'io.swagger.core.v3:swagger-jaxrs2-servlet-initializer-v2-jakarta:2.1.9'
compile 'org.apache.commons:commons-lang3:3.7'
compile 'io.swagger.core.v3:swagger-jaxrs2-jakarta:2.1.7'
compile 'jakarta.ws.rs:jakarta.ws.rs-api:3.0.0'
compile 'jakarta.servlet:jakarta.servlet-api:5.0.0'
}
//resolve {
// outputFileName = 'HfsOpenAPI'
// outputFormat = 'YAML'
// classpath = sourceSets.main.runtimeClasspath
// buildClasspath = classpath
// resourcePackages = ['io.test']
// outputDir = file('main/resources')
//}
Now even after adding metadata into my code, I am not seeing only 404 on HTTP://localhost:8080/api/openapi.yaml
. What is the mistake I am doing?
I was able to solve the issue with proper configuration on build.gradle and servlet configured into Jetty 11 server. The detailed solution is blogged on here - Hooking up openapi with Jetty 11.