【问题标题】:Using swagger-core 2.1.3 with the JDK httpserver将 swagger-core 2.1.3 与 JDK httpserver 一起使用
【发布时间】:2020-07-15 21:02:12
【问题描述】:

我正在使用 Jersey 开发 JAX-RS api,并且正在尝试实现 swagger。我找到了一个资源here,它有我正在寻找的东西,但它已经过时了。有谁知道我如何使用 swagger 2.1.3 做到这一点?

服务器起始代码:

Set<Class<? extends Endpoint>> endpoints = reflections.getSubTypesOf(Endpoint.class);

ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(GsonProvider.class);
resourceConfig.register(GsonExceptionMapper.class);
for (Class<? extends Endpoint> e : endpoints) {
    resourceConfig.register(e);
}

httpServer = JdkHttpServerFactory.createHttpServer(base, resourceConfig, false);
httpExecutor = Executors.newCachedThreadPool();
httpServer.setExecutor(httpExecutor);
httpServer.start();

此代码在启动时产生的错误:

java.lang.NoClassDefFoundError: javax/servlet/ServletConfig
    at java.lang.Class.getDeclaredMethods0(Native Method) ~[?:1.8.0_241]
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[?:1.8.0_241]
    at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[?:1.8.0_241]
    at org.glassfish.jersey.server.model.IntrospectionModeller$2.run(IntrospectionModeller.java:236) ~[?:?]
    at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_241]
    at org.glassfish.jersey.server.model.IntrospectionModeller.getAllDeclaredMethods(IntrospectionModeller.java:230) ~[?:?]
    at org.glassfish.jersey.server.model.IntrospectionModeller.checkForNonPublicMethodIssues(IntrospectionModeller.java:150) ~[?:?]
    at org.glassfish.jersey.server.model.IntrospectionModeller.doCreateResourceBuilder(IntrospectionModeller.java:97) ~[?:?]
    at org.glassfish.jersey.server.model.IntrospectionModeller.access$000(IntrospectionModeller.java:58) ~[?:?]
    at org.glassfish.jersey.server.model.IntrospectionModeller$1.call(IntrospectionModeller.java:90) ~[?:?]
    at org.glassfish.jersey.server.model.IntrospectionModeller$1.call(IntrospectionModeller.java:87) ~[?:?]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:292) ~[?:?]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:274) ~[?:?]
    at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:232) ~[?:?]
    at org.glassfish.jersey.server.model.IntrospectionModeller.createResourceBuilder(IntrospectionModeller.java:87) ~[?:?]
    at org.glassfish.jersey.server.model.Resource.from(Resource.java:781) ~[?:?]
    at org.glassfish.jersey.server.ResourceBagConfigurator.init(ResourceBagConfigurator.java:66) ~[?:?]
    at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:331) ~[?:?]
    at org.glassfish.jersey.server.ApplicationHandler.lambda$initialize$1(ApplicationHandler.java:293) ~[?:?]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:292) ~[?:?]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:274) ~[?:?]
    at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:232) ~[?:?]
    at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:292) ~[?:?]
    at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:259) ~[?:?]
    at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:234) ~[?:?]
    at org.glassfish.jersey.jdkhttp.JdkHttpHandlerContainer.<init>(JdkHttpHandlerContainer.java:72) ~[?:?]
    at org.glassfish.jersey.jdkhttp.JdkHttpServerFactory.createHttpServer(JdkHttpServerFactory.java:84) ~[?:?]
    at fyi.sorenneedscoffee.aurora.Aurora.onEnable(Aurora.java:97) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[patched_1.15.2.jar:git-Paper-365]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:380) ~[patched_1.15.2.jar:git-Paper-365]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:483) ~[patched_1.15.2.jar:git-Paper-365]
    at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugin(CraftServer.java:472) ~[patched_1.15.2.jar:git-Paper-365]
    at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugins(CraftServer.java:386) ~[patched_1.15.2.jar:git-Paper-365]
    at net.minecraft.server.v1_15_R1.MinecraftServer.a(MinecraftServer.java:488) ~[patched_1.15.2.jar:git-Paper-365]
    at net.minecraft.server.v1_15_R1.DedicatedServer.init(DedicatedServer.java:298) ~[patched_1.15.2.jar:git-Paper-365]
    at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:908) ~[patched_1.15.2.jar:git-Paper-365]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_241]
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletConfig
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[?:1.8.0_241]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:171) ~[patched_1.15.2.jar:git-Paper-365]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:100) ~[patched_1.15.2.jar:git-Paper-365]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[?:1.8.0_241]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[?:1.8.0_241]
    ... 37 more```

【问题讨论】:

    标签: java swagger


    【解决方案1】:

    你需要添加依赖:javax.servlet:javax.servlet-api 或者你可以尝试在 servlet 容器中运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-08
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 2017-09-03
      • 2015-09-03
      相关资源
      最近更新 更多