Routing Overloaded Functions in Play Framework 2.0

In Play, when overloading controller methods, these individual methods cannot be redirected more than once, because he does not like the compiler.

Is there any way around this?

Let's say if I had two functions in my controller Product: getBy(String name)and getBy(long id).

And I had two different routes for these functions declared in routes:

GET /p/:id            controllers.Product.getBy(id: Long)
GET /p/:name          controllers.Product.getBy(name: String)

I want to use the "same" function for different routes, is this possible?

+3
source share
1 answer

No, this is not possible, there are two solutions.

First , 2 names are used:

public static Result getByLong(Long id) {
    return ok("Long value: " + id);
}

public static Result getByString(String name) {
    return ok("String value: " + name);
}

also you must use separate routes for it, otherwise you will get a type mismatch

GET   /p-by-long/:id         controllers.Monitor.getByLong(id: Long)
GET   /p-by-string/:name     controllers.Monitor.getByString(name: String)

String , Long

public static Result getByArgOfAnyType(String arg) {
    try {
        Long.parseLong(arg);
        return ok("Long: " + arg);
    } catch (Exception e) {
        return ok("String: " + arg);
    }
}

:

GET   /p/:arg     controllers.Monitor.getByArgOfAnyType(arg : String)

, , , , . , , String , : , String Java?

+3

All Articles