Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] Need help configuring swagger into jetty server

Hello,

I am trying to add a swagger configuration into my jetty server but when I try to add the 
ServletContainer and then attach ServletHolder to it, it fails. Seems like ServletContainer is not able to find any such constructor - 

package com.example.hfs;

import com.cloudian.hfs.handlers.*;
import jakarta.servlet.Servlet;
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.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import io.swagger.jaxrs.listing.ApiListingResource;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;
import io.swagger.jaxrs.config.BeanConfig;



public class StartHFS {

public static void main(String[] args) throws Exception {
System.out.println("StartHFS");

// Build the Swagger Bean
buildSwagger();

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

addHandlers(server);

// Start the Server so it starts accepting connections from clients.
server.start();
server.join();

System.out.println("StartHFS DONE");
}

static void addHandlers(final Server server) throws Exception {
ContextHandlerCollection contexts = new ContextHandlerCollection();
server.setHandler(contexts);

ContextHandler logHandler = new ContextHandler("/log");
logHandler.setHandler(new LoggingHandler());
contexts.addHandler(logHandler);

ContextHandler helloHandler = new ContextHandler("/hello");
helloHandler.setHandler(new HelloHandler());
contexts.addHandler(helloHandler);

ContextHandler featureStoreHandler = new ContextHandler("/featurestore");
featureStoreHandler.setHandler(new FeatureStoreHandler());
contexts.addHandler(featureStoreHandler);

ContextHandler featureGroupsHandler = new ContextHandler("/featuregroups");
featureGroupsHandler.setHandler(new FeatureGroupsHandler());
contexts.addHandler(featureGroupsHandler);

ContextHandler recordsHandler = new ContextHandler("/FeatureGroup");
recordsHandler.setHandler(new RecordsHandler());
contexts.addHandler(recordsHandler);

// Handler for HFS API, swagger.
contexts.addHandler(buildHfsContext());
}

private static ContextHandler buildHfsContext() {
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.packages(StartHFS.class.getPackage().getName(),
FeatureStoreHandler.class.getPackage().getName(),
FeatureGroupsHandler.class.getPackage().getName(),
HelloHandler.class.getPackage().getName(),
LoggingHandler.class.getPackage().getName(),
RecordsHandler.class.getPackage().getName(),
ApiListingResource.class.getPackage().getName());
ServletContainer servletContainer = new ServletContainer(resourceConfig);
ServletHolder servletHolder = new ServletHolder(servletContainer); //"default", DefaultServlet.class);
ServletContextHandler servletContextHandler = new ServletContextHandler( ServletContextHandler.SESSIONS );
servletContextHandler.setContextPath( "/hfs" );
servletContextHandler.addServlet( servletHolder, "/hfs/*");
return servletContextHandler;
}

}
In above code - 
ServletHolder servletHolder = new ServletHolder(servletContainer);
this line from buildHfsContext() method is failing. I am particularly trying to configure the SwaggerConfig for my main class. And I am referring a solution from https://github.com/SriramKeerthi/swagger-jersey2-jetty

My build.gradle contains dependencies - 
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-servlet:11.0.0'
compile 'org.eclipse.jetty:jetty-util:11.0.0'
compile 'org.springframework.data:spring-data-redis:1.8.0.RELEASE'
compile 'redis.clients:jedis:2.9.0'
implementation group: 'org.json', name: 'json', version: '20201115'
implementation group: 'io.swagger', name: 'swagger-jersey2-jaxrs', version: '1.5.3'
compile 'javax.servlet:servlet-api:2.5'
}
Can someone help me out? I am blocked by this situation at the moment. 

Best,
Aniruddha
========

Back to the top