Skip to main content

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

You are currently replacing the ContextHandlerCollection with your ServletContextHandler, but you don't want to do that. 
By adding the ServletContextHandler into the existing ContextHandlerCollection you will maintain the previous handlers (LoggingHandler, HelloHandler and SampleApi).

Cheers,
Lachlan

On Mon, May 3, 2021 at 11:50 AM Aniruddha Tekade via jetty-users <jetty-users@xxxxxxxxxxx> wrote:
Hi Lachlan,

Would this replace previous handlers from the server?

Best,
Aniruddha
========


On Sun, May 2, 2021 at 6:47 PM Lachlan Roberts <lachlan@xxxxxxxxxxx> wrote:
Aniruddha,

You should not be setting the ServletContextHandler as the handler for the server, but instead just add it to the ContextHandlerCollection.

Try replacing the line 
server.setHandler(ctx); with 
contexts.addHandler(ctx); 

Cheers,
Lachlan

On Mon, May 3, 2021 at 9:22 AM Aniruddha Tekade via jetty-users <jetty-users@xxxxxxxxxxx> wrote:
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 - 

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
========
_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users
_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users
_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users

Back to the top