trouble in creating Play 2 Javascript router

I am using Play! 2.6 and trying to create a javascript router. So I have a MyCheck.java class under my controllers folder:

package com.xxx.console.controllers;

import be.objectify.deadbolt.java.actions.Group;
import be.objectify.deadbolt.java.actions.Restrict;
import com.xxx.entity.pojo.Study;
import play.mvc.Controller;

import javax.inject.Inject;
import java.util.List;

public class MyCheck extends Controller {

    public MyCheck() { }

    public static List<Study> find() {

        List<Study> studies = Study.find();

        return studies;

    }

}

and then under the same controllers directory, I have my router setup as follows:

package com.xxx.console.controllers;

import play.mvc.Controller;
import play.mvc.Result;
import play.routing.JavaScriptReverseRouter;

public class MyCheckRouter extends Controller {

    public Result jsRoutes() {
        return ok(JavaScriptReverseRouter.create("jsRoutes",
                routes.javascript.MyCheck.find()))
                .as("text/javascript");
    }

}

I did all these by following Play! 2.6 document. However, it will not compile and says “cannot resolve symbol MyCheck”.

Could anyone please kindly give me a hint?

Many thanks!

To me looks like the find method is not an Action. It needs to return play.mvc.Result and it needs to exist in the routes file.

Thank you so much for the hint. Could you please point me to some examples? I googled this for a long time, just don’t see any real examples. thanks again.

Your find method should not return List<Study>, nor should it be static.

It should be something like this

	public Result find() {
		// turn List<Study> into json or xml or whatever you want
		return ok("{}").as("application/json");
	}

Or the method can return CompletionStage<Result> if you want to make it async.

And you need to define the route in the routes file like this

GET	    /yourPath    com.xxx.console.controllers.MyCheck.find()

thank you very much for the information. Will try to do this. thanks again.

one more thing if you don’t mind. I have been a Spring/SpringBoot dev for quite a while, and just recently started to use Play!. I find the documentation of Play!, compared to Spring/SpringBoot, is really not nearly as good. So might have to pick up a book to learn more. Do you have any recommendation of a good book?

I’d say not really. The 2 biggest downsides of Play imo are poor documentation and the lack of backwards compatibility. By the lack of backwards compatibility I really mean it. They tend to change some of the fundamental stuff. For example, in Play 2.3 you have to return their Promise class for async actions, and then they switched to the standard Java CompletionStage and got rid of their Promise library completely. Before Play 2.6 (or 2.7?) the Http.Context was ThreadLocal based and it was used everywhere, and then they just got rid of it and the class altogether and now you have to pass around Http.Request yourself. So whatever book you buy is probably already out of date. And just to be clear, I still prefer Play. The lack of backwards compatibility is not necessarily a bad thing.

And because of the lack of backwards compatibility, my suggestion to you as a new Play user is that try to stay away from Play’s helper libraries, like Play’s Json helper and Play WSClient HTTP client. Even Akka had too many breaking changes. Just use other 3rd party libraries that have far more stable APIs.

Thank you so much for the detailed information, and it is very helpful! I did spend some time searching online for a good book, but nothing did stand out for now. This is exactly the thing that worries me - the documentation is not good and there are no books that can keep up with the changes… oh well… life is life.

I for sure miss the springboot life, that was easy and this project I am doing now asks for Play! We are all saying after this project, we are not going with Play! anymore.

Just wondering why they cannot just make the document a little better… Maybe I am spoiled by the nice Spring/SpringBoot document.

Thank you again, and happy coding!