简体   繁体   中英

How to do routing in Play framework?

I am new to Play Framework and trying to setup a very small project in Intellij . So, I followed some basic steps:

  1. Create a new project in Intellij
  2. Run it with play framework and it works (localhost:9000)
  3. Now I have added a new Java class file in Controller package.
  4. Add routes configuration in config/routes file.

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 . What I am doing wrong here?

Play switched to using dependency injection and therefore the way to reference a function in your Controller is:

POST /Test @controllers.TestController.createTest()

Notice the '@'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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