简体   繁体   English

如何在Play框架中进行路由?

[英]How to do routing in Play framework?

I am new to Play Framework and trying to setup a very small project in Intellij . 我是Play Framework新手,并尝试在Intellij设置一个非常小的项目。 So, I followed some basic steps: 因此,我遵循了一些基本步骤:

  1. Create a new project in Intellij 在Intellij中创建一个新项目
  2. Run it with play framework and it works (localhost:9000) 使用play框架运行它并可以正常运行(localhost:9000)
  3. Now I have added a new Java class file in Controller package. 现在,我在Controller程序包中添加了一个新的Java类文件。
  4. Add routes configuration in config/routes file. config/routes文件中添加路由配置。

TestController.java TestController.java

public class TestController extends Controller {

public F.Promise<Result> createTest() {
    return F.Promise.promise(() -> TestEndpoints.createTest(Json.fromJson(request().body().asJson(),
            Test.class))).map((result) -> ok(Json.toJson(result)));
}

Routes 路线

POST    /Test    controllers.TestController.createTest()

When I run the project I got this error: 当我运行项目时,出现以下错误:

value createTest is not a member of object controllers.TestController

Why is this happening. 为什么会这样呢? One thing I just noticed that my Controller folder has Application.scala not Application.java . 我刚刚注意到我的Controller文件夹具有Application.scala而不是Application.java一件事。 What I am doing wrong here? 我在这里做错了什么?

Play switched to using dependency injection and therefore the way to reference a function in your Controller is: 播放切换为使用依赖注入,因此在Controller中引用函数的方式是:

POST /Test @controllers.TestController.createTest() POST /测试@ controllers.TestController.createTest()

Notice the '@' 注意“ @”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM