我在Scala/Scalatra项目中使用Salat来序列化MongoDB对象。这是一个普遍的问题,因为我正试图从我的数据库中提取一个复杂的MongoDB对象,该对象嵌入了我为其创建的case类的不同散列和数组。我可以一个字段接一个字段地进行注释,并弄清楚在这种情况下类被破坏了什么,但这只是一种荒谬的调试方式。我得到的堆栈跟踪如下所示:
DefaultArg.safeValue$lzycompute(Grater.scala:406)
com.novus.salat.DefaultArg.safeValue(Grater.scala:402)
com.novus.salat.ConcreteGrater.safeDefault(Grater.scala:363)
com.novus.salat.ConcreteGrater$$anonfun$5$$anonfun$apply$3.apply(Grater.scala:262)
com.novus.salat.ConcreteGrater$$anonfun$5$$anonfun$apply$3.apply(Grater.scala:262)
scala.Option.orElse(Option.scala:257)
com.novus.salat.ConcreteGrater$$anonfun$5.apply(Grater.scala:262)
com.novus.salat.ConcreteGrater$$anonfun$5.apply(Grater.scala:247)
scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
scala.collection.immutable.List.foreach(List.scala:318)
scala.collection.TraversableLike$class.map(TraversableLike.scala:244)
scala.collection.AbstractTraversable.map(Traversable.scala:105)
com.novus.salat.ConcreteGrater.asObject(Grater.scala:247)
com.novus.salat.transformers.in.DBObjectToInContext$class.transform0(Injectors.scala:333)
com.novus.salat.transformers.in.DBObjectToInContext$class.transform(Injectors.scala:339)
com.novus.salat.transformers.in.package$$anon$1.transform(Injectors.scala:66)
com.novus.salat.transformers.Transformer$$anonfun$1.apply(Transformer.scala:85)
com.novus.salat.transformers.Transformer$$anonfun$1.apply(Transformer.scala:85)
scala.Option.flatMap(Option.scala:170)
com.novus.salat.transformers.Transformer.transform_$bang(Transformer.scala:85)
com.novus.salat.Field.in_$bang(Field.scala:64)
com.novus.salat.ConcreteGrater$$anonfun$5$$anonfun$apply$2.apply(Grater.scala:262)
com.novus.salat.ConcreteGrater$$anonfun$5$$anonfun$apply$2.apply(Grater.scala:262)
scala.Option.flatMap(Option.scala:170)
com.novus.salat.ConcreteGrater$$anonfun$5.apply(Grater.scala:262)
com.novus.salat.ConcreteGrater$$anonfun$5.apply(Grater.scala:247)
scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
scala.collection.immutable.List.foreach(List.scala:318)
scala.collection.TraversableLike$class.map(TraversableLike.scala:244)
scala.collection.AbstractTraversable.map(Traversable.scala:105)
com.novus.salat.ConcreteGrater.asObject(Grater.scala:247)
com.novus.salat.transformers.in.DBObjectToInContext$class.transform0(Injectors.scala:333)
com.novus.salat.transformers.in.DBObjectToInContext$class.transform(Injectors.scala:338)
com.novus.salat.transformers.in.package$$anon$2.com$novus$salat$transformers$in$TraversableInjector$$super$transform(Injectors.scala:123)
com.novus.salat.transformers.in.TraversableInjector$class.transformElement(Injectors.scala:370)
com.novus.salat.transformers.in.package$$anon$2.transformElement(Injectors.scala:123)
com.novus.salat.transformers.in.TraversableInjector$$anonfun$after$1.apply(Injectors.scala:385)
scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
scala.collection.immutable.List.foreach(List.scala:318)
scala.collection.TraversableLike$class.map(TraversableLike.scala:244)
scala.collection.AbstractTraversable.map(Traversable.scala:105)
com.novus.salat.transformers.in.TraversableInjector$class.after(Injectors.scala:385)
com.novus.salat.transformers.in.package$$anon$2.after(Injectors.scala:123)
com.novus.salat.transformers.Transformer$$anonfun$1.apply(Transformer.scala:85)
com.novus.salat.transformers.Transformer$$anonfun$1.apply(Transformer.scala:85)
scala.Option.flatMap(Option.scala:170)
com.novus.salat.transformers.Transformer.transform_$bang(Transformer.scala:85)
com.novus.salat.Field.in_$bang(Field.scala:64)
com.novus.salat.ConcreteGrater$$anonfun$5$$anonfun$apply$2.apply(Grater.scala:262)
com.novus.salat.ConcreteGrater$$anonfun$5$$anonfun$apply$2.apply(Grater.scala:262)
scala.Option.flatMap(Option.scala:170)
com.novus.salat.ConcreteGrater$$anonfun$5.apply(Grater.scala:262)
com.novus.salat.ConcreteGrater$$anonfun$5.apply(Grater.scala:247)
scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
scala.collection.immutable.List.foreach(List.scala:318)
scala.collection.TraversableLike$class.map(TraversableLike.scala:244)
scala.collection.AbstractTraversable.map(Traversable.scala:105)
com.novus.salat.ConcreteGrater.asObject(Grater.scala:247)
com.sportsnetwork.api.models.MLBPlayByPlay$$anonfun$findByGameId$1.apply(MLBPlayByPlay.scala:38)
com.sportsnetwork.api.models.MLBPlayByPlay$$anonfun$findByGameId$1.apply(MLBPlayByPlay.scala:38)
scala.Option.map(Option.scala:145)
com.sportsnetwork.api.models.MLBPlayByPlay$.findByGameId(MLBPlayByPlay.scala:38)
com.sportsnetwork.api.MLBController$$anonfun$3.apply(MLBController.scala:49)
org.scalatra.ScalatraBase$class.org$scalatra$ScalatraBase$$liftAction(ScalatraBase.scala:250)
org.scalatra.ScalatraBase$$anonfun$invoke$1.apply(ScalatraBase.scala:245)
org.scalatra.ScalatraBase$$anonfun$invoke$1.apply(ScalatraBase.scala:245)
org.scalatra.ApiFormats$class.withRouteMultiParams(ApiFormats.scala:175)
com.sportsnetwork.api.MLBController.withRouteMultiParams(MLBController.scala:20)
org.scalatra.ScalatraBase$class.invoke(ScalatraBase.scala:244)
com.sportsnetwork.api.MLBController.org$scalatra$json$JsonSupport$$super$invoke(MLBController.scala:20)
org.scalatra.json.JsonSupport$$anonfun$invoke$1.apply(JsonSupport.scala:65)
org.scalatra.json.JsonSupport$$anonfun$invoke$1.apply(JsonSupport.scala:57)
org.scalatra.ApiFormats$class.withRouteMultiParams(ApiFormats.scala:175)
com.sportsnetwork.api.MLBController.withRouteMultiParams(MLBController.scala:20)
org.scalatra.json.JsonSupport$class.invoke(JsonSupport.scala:57)
com.sportsnetwork.api.MLBController.invoke(MLBController.scala:20)
org.scalatra.ScalatraBase$$anonfun$runRoutes$1$$anonfun$apply$7.apply(ScalatraBase.scala:220)
org.scalatra.ScalatraBase$$anonfun$runRoutes$1$$anonfun$apply$7.apply(ScalatraBase.scala:218)
scala.Option.flatMap(Option.scala:170)
org.scalatra.ScalatraBase$$anonfun$runRoutes$1.apply(ScalatraBase.scala:218)
org.scalatra.ScalatraBase$$anonfun$runRoutes$1.apply(ScalatraBase.scala:217)
scala.collection.immutable.Stream.flatMap(Stream.scala:442)
org.scalatra.ScalatraBase$class.runRoutes(ScalatraBase.scala:217)
org.scalatra.ScalatraServlet.runRoutes(ScalatraServlet.scala:49)
org.scalatra.ScalatraBase$class.runActions$1(ScalatraBase.scala:163)
org.scalatra.ScalatraBase$$anonfun$executeRoutes$1.apply$mcV$sp(ScalatraBase.scala:175)
org.scalatra.ScalatraBase$$anonfun$executeRoutes$1.apply(ScalatraBase.scala:175)
org.scalatra.ScalatraBase$$anonfun$executeRoutes$1.apply(ScalatraBase.scala:175)
org.scalatra.ScalatraBase$class.org$scalatra$ScalatraBase$$cradleHalt(ScalatraBase.scala:190)
org.scalatra.ScalatraBase$class.executeRoutes(ScalatraBase.scala:175)
org.scalatra.ScalatraServlet.executeRoutes(ScalatraServlet.scala:49)
org.scalatra.ScalatraBase$$anonfun$handle$1.apply$mcV$sp(ScalatraBase.scala:113)
org.scalatra.ScalatraBase$$anonfun$handle$1.apply(ScalatraBase.scala:113)
org.scalatra.ScalatraBase$$anonfun$handle$1.apply(ScalatraBase.scala:113)
scala.util.DynamicVariable.withValue(DynamicVariable.scala:57)
org.scalatra.DynamicScope$class.withResponse(DynamicScope.scala:80)
org.scalatra.ScalatraServlet.withResponse(ScalatraServlet.scala:49)
org.scalatra.DynamicScope$$anonfun$withRequestResponse$1.apply(DynamicScope.scala:60)
scala.util.DynamicVariable.withValue(DynamicVariable.scala:57)
org.scalatra.DynamicScope$class.withRequest(DynamicScope.scala:71)
org.scalatra.ScalatraServlet.withRequest(ScalatraServlet.scala:49)
org.scalatra.DynamicScope$class.withRequestResponse(DynamicScope.scala:59)
org.scalatra.ScalatraServlet.withRequestResponse(ScalatraServlet.scala:49)
org.scalatra.ScalatraBase$class.handle(ScalatraBase.scala:111)
org.scalatra.ScalatraServlet.org$scalatra$servlet$ServletBase$$super$handle(ScalatraServlet.scala:49)
org.scalatra.servlet.ServletBase$class.handle(ServletBase.scala:43)
com.sportsnetwork.api.MLBController.org$scalatra$scalate$ScalateSupport$$super$handle(MLBController.scala:20)
org.scalatra.scalate.ScalateSupport$class.handle(ScalateSupport.scala:122)
com.sportsnetwork.api.MLBController.org$scalatra$CorsSupport$$super$handle(MLBController.scala:20)
org.scalatra.CorsSupport$$anonfun$handle$1.apply$mcV$sp(CORSSupport.scala:202)
org.scalatra.CorsSupport$$anonfun$handle$1.apply(CORSSupport.scala:190)
org.scalatra.CorsSupport$$anonfun$handle$1.apply(CORSSupport.scala:190)
scala.util.DynamicVariable.withValue(DynamicVariable.scala:57)
org.scalatra.DynamicScope$class.withResponse(DynamicScope.scala:80)
org.scalatra.ScalatraServlet.withResponse(ScalatraServlet.scala:49)
org.scalatra.DynamicScope$$anonfun$withRequestResponse$1.apply(DynamicScope.scala:60)
scala.util.DynamicVariable.withValue(DynamicVariable.scala:57)
org.scalatra.DynamicScope$class.withRequest(DynamicScope.scala:71)
org.scalatra.ScalatraServlet.withRequest(ScalatraServlet.scala:49)
org.scalatra.DynamicScope$class.withRequestResponse(DynamicScope.scala:59)
org.scalatra.ScalatraServlet.withRequestResponse(ScalatraServlet.scala:49)
org.scalatra.CorsSupport$class.handle(CORSSupport.scala:188)
com.sportsnetwork.api.MLBController.handle(MLBController.scala:20)
org.scalatra.ScalatraServlet.service(ScalatraServlet.scala:54)
javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:669)
org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:455)
org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:560)
org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1072)
org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:382)
org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1006)
org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255)
org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
org.eclipse.jetty.server.Server.handle(Server.java:361)
org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:485)
org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:926)
org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:988)
org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:635)
org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
java.lang.Thread.run(Thread.java:701)
我的案例类的一个例子如下:
case class MLBPlayByPlay(datetime: DateTime, gamedate: DateTime, game_id: Int, home_team_id: Int, away_team_id: Int,
home_team_score: Int, away_team_score: Int, sport: String, season_year: Int, season_type: String, location: String, status: String,
home_team_abbr: String, away_team_abbr: String, hp_umpire: String, fb_umpire: String, sb_umpire: String, tb_umpire: String,
play_by_play_seqs: Seq[MLBPlayByPlaySeq], created_at: Option[Date] = None, updated_at: Date)
case class MLBPlayByPlaySeq(batter: Option[MLBBatter] = None, next_batter: Option[MLBBatter] = None, pitchers: Option[Seq[MLBPitcher]] = None, pitches: Option[Seq[MLBPitches]] = None, fielders: Option[Seq[MLBFielder]] = None,
seq_id: Int, inning: Option[Int] = None, outs: Option[Int] = None, no_play: Option[Int] = None, home_team_score: Int, away_team_score: Int)
case class MLBBatter(batter_id: Int, team_id: Int, first_name: String, last_name: String, action: Option[String] = None, out: Option[Int] = None, adv: Option[Int] = None,
to_base: Option[Int] = None, scored: Option[Int] = None, bats: Option[String] = None, appear: Option[Int] = None, pos: Option[String] = None,
pitcher_id: Option[Int] = None, pthrows: Option[String] = None, error_ids: Option[String] = None, location: Option[String] = None, traj: Option[String] = None,
field: Option[String] = None, coords: Option[String] = None, ab: Option[Int] = None, gndout: Option[Int] = None)
case class MLBPitcher(pitcher_id: Int, team_id: Int, first_name: String, last_name: String, inning: Int, outs: Int, throws: String, bbats: String, ab: Int, bf: Int)
case class MLBFielder(fielder_id: Int, team_id: Int, first_name: String, last_name: String, pos: String, assist: Int, put_out: Int)
case class MLBPitches(count: Int, total_pitches: Int, balls: Int, strikes: Int, value: String, batter_id: Int, pitcher_id: Int, runner_1_id: Int, runner_2_id: Int,
runner_3_id: Int, final_pitch: String)
我从MLBPlayByPlay
案例类开始,通过逐字段注释来调试这一点,并使API响应良好。最终,当我取消对play_by_play_seqs: Seq[MLBPlayByPlaySeq]
的注释时,一切都崩溃了。现在我必须进入MLBPlayByPlaySeq案例类,它嵌入了自己的案例类类型,并开始在那里逐一注释。。。肯定有更简单的方法来调试Salat吗?这就是我使用磨碎机的方式:
val res = MLBPlayByPlayDAO.findByGameId(gameId)
res.map(grater[MLBPlayByPlay].asObject(_))
实际的例外情况:
k.api.MLBController - exception occured: com.novus.salat.DefaultArg.safeValue$lzycompute(Grater.scala:406)
将在我从字段中删除Option[Seq[T]]
后不久更新线程。
**更新2*
在从案例类中删除所有Option[Seq[T]]
之后,我将问题缩小到next_batter
是MLBPlayByPlaySeqs
案例类数据库中的空Hash。我将定义从next_batter: Option[MLBBatter] = None
(这会导致它在空哈希上爆炸)更改为next_batter: Map[String, Any] = Map.empty
,它就工作了。这里的问题是:
- 我使用地图失去了类型安全
- 我再也无法手动选择要在序列化的JSON输出中列出的属性了。嵌入式Hash的created_at和updated_at之类的东西出现在我的JSON中,这些都是我在case类中明确遗漏的,我不能通过将
next_batter
更改为Map vs case类来完成
Salat作者在这里-你能分享堆栈跟踪中的实际异常吗?
Salat不支持包含集合的选项——我看到MLBPlayByPlaySeq
中有一些这样的选项。试着把它们变成默认值为Seq.empty
的Seq
,看看这是否能解决问题。
如果没有,请发布实际的异常,我会再看一次。