Forum Discussion

anirtek's avatar
anirtek
Occasional Contributor
3 years ago

What swagger should I use with Jersey 3.0.2 REST Framework?

I am working on a code-first approach in following environment:

  • Jetty 11
  • Jersey 3.0.2
  • Jakarta.ws.rs (since Jetty 11 is part of Jakarta EE 9 big bang)
  • Gradle 7.0

I configured the jersey rest framework in the jetty server and it is already detecting all my APIs. This also means I am done annotating my APIs. Now I need to hook up swagger-core into my project. And I am referring -

In one of the links above, it has been mentioned that: Depending on the way Jersey is configured in your web service, you could hook up Swagger Core to your application using Spring, the Jersey’s container Servlet, or manually.

 

Since I am using Jersey 3.0.2 REST Framework into Jetty, what swagger-core version should I use to hook up swagger into my jersey container's servlet? Or is there a better approach to solve this issue easily? 

2 Replies

  • anirtek's avatar
    anirtek
    Occasional Contributor

    can swagger work with Jersey 3.x? If not, what other way is there I can get this to work? 

  • anirtek's avatar
    anirtek
    Occasional Contributor

    The main issue was in annotations of the code that I was using spring based annotations and using jakarta.ws.rs namespace. Here are my steps that solved the issue:

     

    1. 1. Upgraded the gradle as mentioned in the bottom
    2. 2. Fixed annotations
    3. 3. Configure jersey servlet and openapi servlet from swagger-core as mentioned below
    4. 4. Added content of `swagger-ui/dist/*` into `/src/resources/webapp` and pointed the index.html to `localhost:8080/openapi/swagger.json` where my openapi.yaml is being generated

    Build.gradle:

    plugins {
        id 'java'
        id 'application'
        id "io.swagger.core.v3.swagger-gradle-plugin" version "2.1.9"
    }
    
    group 'org.example'
    version '1.0-SNAPSHOT'
    
    repositories {
        mavenCentral()
    }
    
    ext {
        javaMainClass = "io.swagger.Main"
    }
    
    application {
        mainClassName = javaMainClass
    }
    dependencies {
        implementation platform('org.glassfish.jersey:jersey-bom:3.0.2')
        implementation 'org.glassfish.jersey.containers:jersey-container-grizzly2-http'
        implementation 'org.glassfish.jersey.inject:jersey-hk2'
        implementation 'org.glassfish.jersey.media:jersey-media-json-binding'
        implementation 'org.apache.commons:commons-lang3:3.7'
        implementation 'io.swagger.core.v3:swagger-jaxrs2-jakarta:2.1.9'
        implementation 'jakarta.ws.rs:jakarta.ws.rs-api:3.0.0'
        implementation 'jakarta.servlet:jakarta.servlet-api:5.0.0'
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
    }
    
    resolve {
        outputFileName = 'MyRestAPI'
        outputFormat = 'JSON'
        prettyPrint = 'TRUE'
        classpath = sourceSets.main.runtimeClasspath
        buildClasspath = classpath
        resourcePackages = ['io.swagger']
        outputDir = file('test')
    }

     

    Jersey & openapi servlet into Jetty Server:

    // Setup Jetty Servlet
    ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
    servletContextHandler.setContextPath("/");
    contexts.addHandler(servletContextHandler);

    // Setup API resources
    ServletHolder jersey = servletContextHandler.addServlet(ServletContainer.class, "/api/*");
    jersey.setInitOrder(1);
    jersey.setInitParameter("jersey.config.server.provider.packages",
    "com.cloudian.hfs.handlers;io.swagger.v3.jaxrs2.integration.resources");

    // Expose API definition independently into yaml/json
    ServletHolder openApi = servletContextHandler.addServlet(OpenApiServlet.class, "/openapi/*");
    openApi.setInitOrder(2);
    openApi.setInitParameter("openApi.configuration.resourcePackages",
    "com.cloudian.hfs.handlers");

    // Setup Swagger-UI static resources
    String resourceBasePath = ServiceLoader.class.getResource("/webapp").toExternalForm();
    servletContextHandler.setWelcomeFiles(new String[] {"index.html"});
    servletContextHandler.setResourceBase(resourceBasePath);
    servletContextHandler.addServlet(new ServletHolder(new DefaultServlet()), "/*"); 

    And then I was able to access swagger.json at http://localhost:8080/