2016-01-19 68 views
1

我有以下的重載函數,以配合我路線文件如何定義的播放框架路線可選路徑參數

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] 
+0

您可以給GET請求中未給出的參數賦予默認值,您知道嗎? Play不支持該選項作爲路由文件中的參數。 – alifirat

+2

您只能有可選的查詢字符串參數,但不能包含可選的路徑參數。 –

回答

4

你在你的控制器只需要一個方法,像這樣:

package controllers 

import play.api.mvc._ 

class Application extends Controller { 

    def options(id:Long, revision: Int, transaction: Int) = Action { 
    Ok(views.html.index(s"id: $id, revision: $revision, transaction: $transaction")) 
    } 
} 

然後你就可以宣佈你的路線是這樣的:

GET  /options        controllers.Application.options(id:Long = 1, revision:Int = 2, transaction:Int = 3) 
GET  /options/:id       controllers.Application.options(id:Long, revision:Int = 2, transaction:Int = 3) 
GET  /options/:id/:revision    controllers.Application.options(id:Long, revision:Int, transaction:Int = 3) 
GET  /options/:id/:revision/:transaction controllers.Application.options(id:Long, revision:Int, transaction:Int) 
+0

謝謝!但對我來說,這些默認值也是問題,因爲我將不得不根據這些字段(id,revision,transaction)來查詢mondoDB。具有默認值意味着我的查詢可能會產生不同的結果。 – summerNight

0

您不需要在路徑文件中定義3個條目。只有一個條目具有以下給定簽名:

controllers.Application.options(id:Option[Long], revision:Option[Int], transaction:Option[Int]) 

而在控制器中,只需使用id.isDefined, revision.isDefined, transaction.isDefined就可以這樣檢查全部3個。然後您可以使用.get方法獲取該值。