如何使用 Jetty 11 运行 jakarta EE 9 命名空间配置 swagger?
How to configure swagger with Jetty 11 running with jakarta EE 9 namespace?
我是 swagger 世界的新手,对如何从我的 api 配置 openapi.yaml 感到困惑。我正在采用 CODE FIRST 方法,我们使用 swagger 记录现有的 API。由于我的服务器是 Jetty 11,它不能与 javax.servlet
、javax.*
和 swagger.jersey2.jaxrs
依赖项一起工作。
遵循 Swagger 2.X Integration and Configuration and following the configuration for hooking swagger with jetty from Swagger Setup for Embedded Jetty Server 的指南后,我使用了以下方法 -
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");
}
我的 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')
//}
现在,即使在我的代码中添加了元数据之后,我也不会在 HTTP://localhost:8080/api/openapi.yaml
上只看到 404。我做错了什么?
我能够通过在 build.gradle 上进行正确配置并将 servlet 配置到 Jetty 11 服务器中来解决问题。详细的解决方案在此处发布 - Hooking up openapi with Jetty 11.
我是 swagger 世界的新手,对如何从我的 api 配置 openapi.yaml 感到困惑。我正在采用 CODE FIRST 方法,我们使用 swagger 记录现有的 API。由于我的服务器是 Jetty 11,它不能与 javax.servlet
、javax.*
和 swagger.jersey2.jaxrs
依赖项一起工作。
遵循 Swagger 2.X Integration and Configuration and following the configuration for hooking swagger with jetty from Swagger Setup for Embedded Jetty Server 的指南后,我使用了以下方法 -
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");
}
我的 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')
//}
现在,即使在我的代码中添加了元数据之后,我也不会在 HTTP://localhost:8080/api/openapi.yaml
上只看到 404。我做错了什么?
我能够通过在 build.gradle 上进行正确配置并将 servlet 配置到 Jetty 11 服务器中来解决问题。详细的解决方案在此处发布 - Hooking up openapi with Jetty 11.