我正在遵循《 Scalatra Swagger指南》(http://scalatra.org//guides/2.6/swagger.html),但是在我自己的代码中无法获得相同的结果。 SwaggerSupport某种程度上无法处理我的案例类。
import org.scalatra._
import org.slf4j.LoggerFactory
import org.json4s.{DefaultFormats, Formats}
import org.scalatra.json.NativeJsonSupport
import org.scalatra.swagger.{Swagger, SwaggerSupport}
class SproutServlet(implicit val swagger: Swagger)
extends ScalatraServlet with NativeJsonSupport with SwaggerSupport {
...
case class Hello(id: String = UUID.randomUUID().toString, hello: String = "hello")
val hello = (apiOperation[Hello] ("hello")
summary "Hello"
description "Description for hello")
get("/hello",operation(hello)) {
Hello()
}
}
它在我执行apiOperation[Hello]
的地方抛出NullPointerException。如果我使用String
而不是我的案例类Hello
代替,它会起作用(没有stacktrace)。如何获取该apiOperation表达式以接受我的案例类?
完整的堆栈跟踪:
2019-04-14 21:28:50.995:WARN:oejw.WebAppContext:main: Failed startup of context o.e.j.w.WebAppContext@629f0666{/,file:///Users/robert.kuhar/dev/sprout/target/webapp/,UNAVAILABLE}{file:///Users/robert.kuhar/dev/sprout/target/webapp/}
java.lang.NullPointerException
at org.scalatra.swagger.reflect.ScalaSigReader$.$anonfun$findScalaSig$1(ScalaSigReader.scala:133)
at scala.Option.orElse(Option.scala:306)
at org.scalatra.swagger.reflect.ScalaSigReader$.findScalaSig(ScalaSigReader.scala:133)
at org.scalatra.swagger.reflect.ScalaSigReader$.$anonfun$findScalaSig$1(ScalaSigReader.scala:133)
at scala.Option.orElse(Option.scala:306)
at org.scalatra.swagger.reflect.ScalaSigReader$.findScalaSig(ScalaSigReader.scala:133)
at org.scalatra.swagger.reflect.ScalaSigReader$.findClass(ScalaSigReader.scala:41)
at org.scalatra.swagger.reflect.ScalaSigReader$.read$1(ScalaSigReader.scala:35)
at org.scalatra.swagger.reflect.ScalaSigReader$.$anonfun$readField$1(ScalaSigReader.scala:35)
at scala.Option.getOrElse(Option.scala:138)
at org.scalatra.swagger.reflect.ScalaSigReader$.read$1(ScalaSigReader.scala:35)
at org.scalatra.swagger.reflect.ScalaSigReader$.readField(ScalaSigReader.scala:37)
at org.scalatra.swagger.reflect.Reflector$.$anonfun$createDescriptor$3(Reflector.scala:92)
at scala.collection.TraversableLike.$anonfun$map$1(TraversableLike.scala:237)
at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:62)
at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:55)
at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:49)
at scala.collection.TraversableLike.map(TraversableLike.scala:237)
at scala.collection.TraversableLike.map$(TraversableLike.scala:230)
at scala.collection.AbstractTraversable.map(Traversable.scala:108)
at org.scalatra.swagger.reflect.Reflector$.fields$1(Reflector.scala:90)
at org.scalatra.swagger.reflect.Reflector$.properties$1(Reflector.scala:105)
at org.scalatra.swagger.reflect.Reflector$.createDescriptor(Reflector.scala:159)
at org.scalatra.swagger.reflect.Reflector$.$anonfun$describe$2(Reflector.scala:44)
at org.scalatra.swagger.reflect.package$Memo.apply(package.scala:16)
at org.scalatra.swagger.reflect.Reflector$.describe(Reflector.scala:44)
at org.scalatra.swagger.Swagger$.modelToSwagger(Swagger.scala:110)
at org.scalatra.swagger.Swagger$.$anonfun$collectModels$8(Swagger.scala:78)
at scala.collection.TraversableLike$WithFilter.$anonfun$flatMap$2(TraversableLike.scala:773)
at scala.collection.immutable.Set$Set2.foreach(Set.scala:132)
at scala.collection.TraversableLike$WithFilter.flatMap(TraversableLike.scala:772)
at org.scalatra.swagger.Swagger$.collectModels(Swagger.scala:76)
at org.scalatra.swagger.Swagger$.collectModels(Swagger.scala:52)
at org.scalatra.swagger.SwaggerSupportSyntax.registerModel(SwaggerSupport.scala:388)
at org.scalatra.swagger.SwaggerSupportSyntax.registerModel$(SwaggerSupport.scala:387)
at com.climate.sprout.SproutServlet.registerModel(SproutServlet.scala:11)
at org.scalatra.swagger.SwaggerSupport.apiOperation(SwaggerSupport.scala:505)
at org.scalatra.swagger.SwaggerSupport.apiOperation$(SwaggerSupport.scala:504)
at com.climate.sprout.SproutServlet.apiOperation(SproutServlet.scala:11)
at com.climate.sprout.SproutServlet.<init>(SproutServlet.scala:40)
at ScalatraBootstrap.init(ScalatraBootstrap.scala:14)
at org.scalatra.servlet.ScalatraListener.configureCycleClass(ScalatraListener.scala:66)
at org.scalatra.servlet.ScalatraListener.contextInitialized(ScalatraListener.scala:22)
at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:890)
at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:532)
at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:853)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:344)
at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1515)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1477)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:785)
at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:261)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:545)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:133)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:115)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:167)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:133)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:115)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:133)
at org.eclipse.jetty.server.Server.start(Server.java:418)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:107)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
at org.eclipse.jetty.server.Server.doStart(Server.java:385)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.runner.Runner.run(Runner.java:530)
at org.eclipse.jetty.runner.Runner.main(Runner.java:571)