Skip to main content

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

Hello Joakim, 

Thank you for the clarification. That is really helpful and I tried making changes to my Gradle as follows:

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 'org.springframework.data:spring-data-redis:1.8.0.RELEASE'
compile 'redis.clients:jedis:3.5.2'
// https://mvnrepository.com/artifact/org.json/json
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-jaxrs2-jakarta', version: '2.1.9'
compile group: 'io.swagger.core.v3', name:'swagger-jaxrs2-servlet-initializer-v2-jakarta', version: '2.1.9'
// compile group: 'io.swagger', name: 'swagger-jersey2-jaxrs', version: '1.6.2'
}
The reason for using swagger-jaxrs2-jakarta is that I want to use OpenAPI 3 and according to the documentation here swagger-core-help-page , from Swagger-core v2.1.9 they are supporting jakarta namespace. 

But when I add dependencies in my jetty server based on above, I am not able to see the swagger.json. Is anything wrong with my way of adding dependencies in code below - 

import com.cloudian.hfs.handlers.FeatureGroupsHandler;
import com.cloudian.hfs.handlers.FeatureStoreHandler;
import com.cloudian.hfs.handlers.RecordsHandler;
import com.cloudian.hfs.handlers.StreamingHandler;
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.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 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");
}
Best,
Aniruddha
========


On Mon, Apr 26, 2021 at 5:45 PM Joakim Erdfelt <joakim@xxxxxxxxxxx> wrote:
Most of your issues are you don't know that Jetty 11 is part of the Jakarta EE 9 "big bang" which changed the namespace from `javax.<spec>` to `jakarta.<spec>`, rendering the old spec namespace invalid and unusable.

> compile 'org.eclipse.jetty:jetty-server:11.0.0'
> compile 'javax.servlet:servlet-api:2.5'

Jetty 11 is a Jakarta EE 9 `jakarta.servlet` container.
That `javax.servlet` dependency is bad, and should not be used at all.
If you have any component needing it, you have a bad component in use for Jetty 11.

> implementation group: 'io.swagger', name: 'swagger-jersey2-jaxrs', version: '1.5.3'

That has a dependency on javax.servlet, so that `swagger-jersey2-jaxrs` will not work with Jetty 11. (You'll want Jetty 10 for that)

> compile 'org.springframework.data:spring-data-redis:1.8.0.RELEASE'
> compile 'redis.clients:jedis:2.9.0'

Those have cdi, javax.servlet, javax.el, and other dependencies that are still on `javax.*` namespace.

In short, you'll either need to upgrade all of your dependencies to ones that use Jakarta EE 9 on `jakarta.<spec>` or downgrade to Jetty 10.
You cannot mix and match both `javax.<spec>` and `jakarta.<spec>` like that.

Joakim Erdfelt / joakim@xxxxxxxxxxx


On Mon, Apr 26, 2021 at 7:07 PM Aniruddha Tekade via jetty-users <jetty-users@xxxxxxxxxxx> wrote:
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
========
_______________________________________________
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