我有以下的重載函數,以配合我路線文件如何定義的播放框架路線可選路徑參數
def getMessage() = getMessage(None, None, None)
def getMessage(campaignID: Option[String]) = getMessage(campaignID, None, None)
def getMessage(campaignID: Option[String], revision: Option[Int]) = getMessage(campaignID, revision, None)
def getMessage(campaignID: Option[String], revision: Option[Int], transactionID: Option[Int]) = Action.async { implicit request =>
db.getDocument(campaignID, revision, transactionID)
.map(result =>
Ok(Json.toJson(result))).recover {case ex: IOException => InternalServerError("Please install MongoDB")}
}
這裏是我的路線文件:
GET /message controllers.Application.getMessage()
GET /message/:campaignID controllers.Application.getMessage(campaignID:String)
GET /message/:campaignID/:revision controllers.Application.getMessage(campaignID:String, revision:Int)
GET /message/:campaignID/:revision/:transactionID controllers.Application.getMessage(campaignID:String, revision:Int, transactionID:Int)
我如何使其工作,以便在REST調用中campaignID,revision和transactionID是完全可選的?現在,我收到以下錯誤:
[error] /Users/superman/conf/routes:9: Compilation error[Using different overloaded methods is not allowed. If you are using a single method in combination with default parameters, make sure you declare them all explicitly.]
[error] GET /message/:campaignID controllers.Application.getMessage(campaignID:String)
[error] ^
[error] (compile:playRoutes) @6oo852mf7: Compilation error in /Users/superman/conf/routes:9
[error] - application -
! @6oo852mf7 - Internal server error, for (GET) [/message/A/1/124] ->
play.sbt.routes.RoutesCompilationException: Compilation error[Using different overloaded methods is not allowed. If you are using a single method in combination with default parameters, make sure you declare them all explicitly.]
at play.sbt.routes.RoutesCompiler$$anonfun$9.apply(RoutesCompiler.scala:152) ~[na:na]
at play.sbt.routes.RoutesCompiler$$anonfun$9.apply(RoutesCompiler.scala:150) ~[na:na]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library-2.11.6.jar:na]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library-2.11.6.jar:na]
at scala.collection.immutable.List.foreach(List.scala:318) ~[scala-library-2.11.6.jar:na]
at scala.collection.TraversableLike$class.map(TraversableLike.scala:244) ~[scala-library-2.11.6.jar:na]
at scala.collection.AbstractTraversable.map(Traversable.scala:105) ~[scala-library-2.11.6.jar:na]
at play.sbt.routes.RoutesCompiler$.compileRoutes(RoutesCompiler.scala:150) ~[na:na]
at play.sbt.routes.RoutesCompiler$$anonfun$4.apply(RoutesCompiler.scala:127) ~[na:na]
at play.sbt.routes.RoutesCompiler$$anonfun$4.apply(RoutesCompiler.scala:126) ~[na:na]
at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47) ~[scala-library-2.11.6.jar:na]
at sbt.$tilde$greater$$anonfun$$u2219$1.apply(TypeFunctions.scala:40) ~[na:na]
at sbt.std.Transform$$anon$4.work(System.scala:63) ~[na:na]
at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:226) ~[na:na]
at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:226) ~[na:na]
at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:17) ~[na:na]
at sbt.Execute.work(Execute.scala:235) ~[na:na]
at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:226) ~[na:na]
at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:226) ~[na:na]
at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:159) ~[na:na]
at sbt.CompletionService$$anon$2.call(CompletionService.scala:28) ~[na:na]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_60]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_60]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_60]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_60]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_60]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_60]
您可以給GET請求中未給出的參數賦予默認值,您知道嗎? Play不支持該選項作爲路由文件中的參數。 – alifirat
您只能有可選的查詢字符串參數,但不能包含可選的路徑參數。 –