结合使用swagger-core 2.1.3和JDK httpserver

时间:2020-07-15 21:02:12

标签: java swagger

我正在使用Jersey来开发JAX-RS api,并且正在尝试实现敏捷。我找到了一个资源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```

1 个答案:

答案 0 :(得分:0)

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