使用动态 http 部分重载路由

Overloading a route with dynamic http parts

我有一条路线:

GET     /latest/:repo/:artifact     controllers.Find.findLatestArtifact(repo: String, artifact: String)

对我们来说就像 restful api。但是现在,我有一个带有 html 表单的新视图,需要将操作发送到该控制器,并使用从表单中选择的两个 html 来填充参数。

我尝试添加另一条路线,例如:

GET     /latest     controllers.Find.findLatestArtifact()

并重载controller方法手动读取http get参数,但不喜欢

以前我已经在这里问过如何在没有 0 args 的控制器中从 html 表单填写参数:

看来这不可能。那么,我该如何解决这个问题,而不必重命名控制器方法?

编辑: 我已经为您的其他问题提供了答案,但与此无关的干净解决方案。 你实际上可以用

之类的东西使路线超载
GET     /latest                     controllers.Find.findLatestArtifact()
GET     /latest/:repo/:artifact     controllers.Find.findLatestRepoArtifact(repo: String, artifact: String)

确保它们以正确的顺序列出。显然这些将路由到不同的方法(这是更干净的服务器端并且更能描述方法的实际作用),因此在您的代码中您需要一个简单的重定向或只是 return 重载方法的结果:

public static Result findLatestArtifact(){
        return findLatestRepoArtifact("DefaultRepo","DefaultArtifact");
}

public static Result findLatestRepoArtifact(String repo, String artifact){
               ... some code here ...
}

或者你可以用其他方式来做 ()