我尝试使用Kotlin 1.0.3在Dropwizard 1.0中使用子资源实现资源。我有一个示例资源:
package net.reznik.stackoverflow.resources
import javax.ws.rs.Consumes
import javax.ws.rs.Path
import javax.ws.rs.PathParam
import javax.ws.rs.Produces
import javax.ws.rs.core.MediaType
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
class TestResource {
@Path("/{foo}/")
fun subresource(@PathParam("foo") foo: String): Any {
return TestSubResource()
}
}
还有一个子资源:
package net.reznik.stackoverflow.resources
import javax.ws.rs.POST
import javax.ws.rs.Path
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.Response
class TestSubResource {
@POST
@Path("/bar")
fun bar(request: String): Response {
return Response.ok("entity received: $request", MediaType.TEXT_PLAIN_TYPE).build()
}
}
如果我将子资源函数的返回类型从Any
更改为TestSubResource
,那么一切都按预期工作。我想使用Any
作为返回类型,以便根据{foo}
的值返回不同的子资源。
使用Any
时,dropwizard会在启动时因长堆栈跟踪而爆炸......
WARN [2016-08-31 22:01:32,454] /: unavailable
! java.lang.NullPointerException: null
! at io.dropwizard.jersey.DropwizardResourceConfig$EndpointLogger.populate(DropwizardResourceConfig.java:173)
! at io.dropwizard.jersey.DropwizardResourceConfig$EndpointLogger.populate(DropwizardResourceConfig.java:164)
! at io.dropwizard.jersey.DropwizardResourceConfig$EndpointLogger.populate(DropwizardResourceConfig.java:189)
! at io.dropwizard.jersey.DropwizardResourceConfig$EndpointLogger.populate(DropwizardResourceConfig.java:164)
! at io.dropwizard.jersey.DropwizardResourceConfig$EndpointLogger.populate(DropwizardResourceConfig.java:159)
! at io.dropwizard.jersey.DropwizardResourceConfig.getEndpointsInfo(DropwizardResourceConfig.java:130)
! at io.dropwizard.jersey.DropwizardResourceConfig.logComponents(DropwizardResourceConfig.java:80)
! at io.dropwizard.jersey.DropwizardResourceConfig$ComponentLoggingListener.onEvent(DropwizardResourceConfig.java:245)
! at org.glassfish.jersey.server.internal.monitoring.CompositeApplicationEventListener.onEvent(CompositeApplicationEventListener.java:74)
! at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:629)
! at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:184)
! at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:350)
! at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:347)
! at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
! at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
! at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:255)
! at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:347)
! at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:392)
! at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177)
! at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:369)
! at javax.servlet.GenericServlet.init(GenericServlet.java:244)
! at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:640)
! at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:419)
! at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:875)
! at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:349)
! at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:772)
! at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:262)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
! at com.codahale.metrics.jetty9.InstrumentedHandler.doStart(InstrumentedHandler.java:103)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
! at org.eclipse.jetty.server.handler.gzip.GzipHandler.doStart(GzipHandler.java:231)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
! at org.eclipse.jetty.server.handler.StatisticsHandler.doStart(StatisticsHandler.java:252)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
! at org.eclipse.jetty.server.Server.start(Server.java:411)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
! at org.eclipse.jetty.server.Server.doStart(Server.java:378)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
! at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:53)
! at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:44)
! at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:85)
! at io.dropwizard.cli.Cli.run(Cli.java:75)
! at io.dropwizard.Application.run(Application.java:79)
有没有办法用kotlin / dropwizard实现子资源定位器,这样我就可以根据自己的自定义逻辑动态返回多个子资源中的一个 - 即使用返回类型Any
?
答案 0 :(得分:0)
当Jersey使用Polymorphic资源返回一个也可能是资源的类型时,它会在请求期间在运行时解析此子资源。 DropWizard几乎按原样使用Jersey,因此会支持这种用例。
但是,DropWizard有一个功能来记录正在进行的路由绑定的输出,它输出它在Servlet init期间确定的资源配置。这段代码有一个bug。我在这里报告了确切的错误:
https://github.com/dropwizard/dropwizard/issues/1716
我没有看到一个简单的解决方法,例如关闭此日志记录调用或取消注册该事件。看起来需要对DropWizard进行补丁。
更新:此问题已通过拉取请求https://github.com/dropwizard/dropwizard/pull/1718解决,如果您使用该合并跟踪下一版本master
,则会包含此修复程序。