简体   繁体   English

如何配置 swagger 与 Jetty 11 运行 jakarta EE 9 命名空间?

[英]How to configure swagger with Jetty 11 running with jakarta EE 9 namespace?

I am very new to swagger world and getting confused about how to go around configuring openapi.yaml from my apis.我对 swagger 世界很陌生,并且对如何从我的 api 配置 openapi.yaml 感到困惑。 I am doing a CODE FIRST approach where we document the existing APIs using swagger.我正在使用 CODE FIRST 方法,我们使用 swagger 记录现有的 API。 Since my server is Jetty 11, it can not work with javax.servlet , javax.* and swagger.jersey2.jaxrs dependencies.由于我的服务器是 Jetty 11,它不能与javax.servletjavax.*swagger.jersey2.jaxrs依赖项一起使用。

After following the guidelines from Swagger 2.X Integration and Configuration and following the configuration for hooking swagger with jetty from Swagger Setup for Embedded Jetty Server , I used the following approach -在遵循Swagger 2.X 集成和配置中的指南并遵循用于将 swagger 与来自Swagger 的码头挂钩的配置后,我使用了嵌入式码头服务器的以下方法设置

package com.example.hfs;

import com.example.hfs.*
import jakarta.servlet.Servlet;
import jakarta.servlet.ServletContainerInitializer;
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 class HFS {
    static public Properties props;

    // Constants
    final static public String RESPONSE_CONTENT_TYPE_JSON = "application/json;charset=UTF-8";
    //final static public String RESPONSE_CONTENT_TYPE_TEXT = "text/html;charset=UTF-8";

    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");
    }

And my gradle contains following dependencies:我的 gradle 包含以下依赖项:

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 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-core-jakarta', version: '2.1.9'
    implementation 'io.swagger.core.v3:swagger-jaxrs2-jakarta:2.1.9'
    implementation 'io.swagger.core.v3:swagger-jaxrs2-servlet-initializer-v2-jakarta:2.1.9'
    compile 'org.apache.commons:commons-lang3:3.7'
    compile 'io.swagger.core.v3:swagger-jaxrs2-jakarta:2.1.7'
    compile 'jakarta.ws.rs:jakarta.ws.rs-api:3.0.0'
    compile 'jakarta.servlet:jakarta.servlet-api:5.0.0'
}

//resolve {
//    outputFileName = 'HfsOpenAPI'
//    outputFormat = 'YAML'
//    classpath = sourceSets.main.runtimeClasspath
//    buildClasspath = classpath
//    resourcePackages = ['io.test']
//    outputDir = file('main/resources')
//}

Now even after adding metadata into my code, I am not seeing only 404 on HTTP://localhost:8080/api/openapi.yaml .现在,即使将元数据添加到我的代码中,我在HTTP://localhost:8080/api/openapi.yaml上也只看到 404。 What is the mistake I am doing?我在做什么错误?

I was able to solve the issue with proper configuration on build.gradle and servlet configured into Jetty 11 server.我能够通过在 build.gradle 和配置到 Jetty 11 服务器中的 servlet 上的正确配置来解决这个问题。 The detailed solution is blogged on here - Hooking up openapi with Jetty 11 .详细的解决方案在此处发布 - Hooking up openapi with Jetty 11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM