Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 路由过载不';行不通_Scala_Playframework 2.1_Playframework 2.2 - Fatal编程技术网

Scala 路由过载不';行不通

Scala 路由过载不';行不通,scala,playframework-2.1,playframework-2.2,Scala,Playframework 2.1,Playframework 2.2,我希望能够做到这一点: POST /items controllers.Application.update() POST /items/:itemType controllers.Application.update(itemType: String) POST /items/:itemType/:id controllers.Application.update(itemType: String, id: Int) 但由于方法更新定义了两次,因此无法编译。然后我改变了它,它也没有: POS

我希望能够做到这一点:

POST /items controllers.Application.update()
POST /items/:itemType controllers.Application.update(itemType: String)
POST /items/:itemType/:id controllers.Application.update(itemType: String, id: Int)
但由于
方法更新定义了两次,因此无法编译。然后我改变了它,它也没有:

POST /items controllers.Application.update(itemType: Option[String] = None, id: Option[Int] = None)
POST /items/:itemType controllers.Application.update(itemType: String, id: Option[Int] = None)
POST /items/:itemType/:id controllers.Application.update(itemType: String, id: Int)
错误是:

  • 前一个
  • 类型不匹配;找到:选项[字符串];必需:字符串
  • 我该怎么办?我不想做这样的事:

    POST /items controllers.Application.updateAll()
    POST /items/:itemType controllers.Application.updateByType(itemType: String)
    POST /items/:itemType/:id controllers.Application.updateByTypeAndId(itemType: String, id: Int)
    
    这个看起来也不太好,因为我想用
    选项来代替空字符串:

    POST /items controllers.Application.update(itemType: String = "", id: Int = "")
    POST /items/:itemType/:id controllers.Application.update(itemType: String, id: Int = "")
    POST /items/:itemType/:id controllers.Application.update(itemType: String, id: Int)
    

    不幸的是,对选项的支持似乎在v2中被删除了—例如,请参阅—因此,您可能需要编写自己的
    PathBindable
    来处理选项(如上面的链接所述),或者您注意到的其他不好的选择之一。

    如果您能够更改URL格式,您就可以使用选项

    路由:
    POST/items控制器.Application.update(itemType:Option[String],id:Option[Int])

    URL:
    http://localhost:9000/items?itemType=someItem&id=123


    使用此格式,您可以在进行web服务调用时省略itemType、id或两者。

    我可以将参数从url强制转换为特定类型吗<代码>(itemType:MyClass1,id:MyClass2)
    对不起,我自己从来没有研究过,所以我不能说。我希望有“/”而不是“?”。我明白。那样更干净。我想提出一个选项,以防您(或其他在未来寻找解决方案的人)需要使用选项。