Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] Adding different handlers into ContextHandlerCollection

Hello all,

I am using Jetty 11 (Jakarta namespace ) and trying to integrate 
swagger into it. I am very new to swagger as well as Jetty, I am 
referring a github gist - 
https://gist.github.com/nosmokingpistol/302c4c3ef30f183cf70e

When I looked at the examples of the swagger integration into Jetty,
most of the projects create a ServletContextHandler and then add it into 
ContextHandlerCollection. And this seems to be the easiest option to work
with. 

However, in my case, there are already pre-existing handlers of type 
ContextHandler added into contextHandlerCollection. So when I add 
my ServletContextHandler into ContextHandlerCollection and run the program, 
it fails at:

Caused by: java.lang.IllegalStateException: InjectionManagerFactory not found.

I need help with understanding how I can add different handlers into Jetty 11's 
ContextHandlerCollection and make it run successfully?
 
My server main method :
--------------------------
public static void main(String[] args) throws Exception {
System.out.println("StartHFS");

// 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 apiHandler = new ContextHandler("/api");
apiHandler.setHandler(new SampleApi());
contexts.addHandler(apiHandler);
        ServletContextHandler ctx = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
ctx.setContextPath("/");
server.setHandler(ctx);
ServletHolder servletHolder = ctx.addServlet(ServletContainer.class, "/rest/*");
servletHolder.setInitOrder(1);
servletHolder.setInitParameter("jersey.config.server.provider.packages", "com.cloudian.handlers");
}
--------------------------
My dependencies are from Jakarta 9 namespace (even for swagger-core/openapi3.0):
--------------------------
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'
implementation group: 'org.json', name: 'json', version: '20201115'

// Jersey dependencies to setup jersey with Jetty
implementation 'org.glassfish.jersey.containers:jersey-container-jetty-http:3.0.2'
implementation 'org.glassfish.jersey.core:jersey-server:3.0.2'
implementation 'org.glassfish.jersey.containers:jersey-container-servlet-core:3.0.2'

// alternative to jaxrx.ws.rs for Jetty 11
implementation 'jakarta.ws.rs:jakarta.ws.rs-api:3.0.0'

// Swagger core dependencies
implementation 'io.swagger.core.v3:swagger-jaxrs2-jakarta:2.1.9'
}
--------------------------
How do I solve this issue? How do I make Jetty run even after adding servletContextHandler into ContextHandlerCollection? What is the mistake I am doing here?
Best,
Aniruddha
========

Back to the top