Overload Method in Application.java Play Framework

How to set overload method in Application.java in Play Project?

Here is an example of what I'm doing right now:

Application.java

public class Application extends Controller {

    public static void index() {
        render();
    }

    public static void getData() {
        renderText("Without Parameter");
    }

    public static void getData(String name) {
        renderText("With Parameter name = " + name);
    }
}

<strong> routes

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET     /                                       Application.index
GET     /data                                   Application.getData

# Ignore favicon requests
GET     /favicon.ico                            404

# Map static resources from the /app/public folder to the /public path
GET     /public/                                staticDir:public

# Catch all
*       /{controller}/{action}                  {controller}.{action}

Test:

  • Call getDatawithout a parameterhttp://localhost:9000/data
  • Call getDatawith parameterhttp://localhost:9000/data?name=test

Result:

  • With parameter name = null
  • With parameter name = test

What I want for the result:

  • No parameter
  • With parameter name = test

I am very grateful to you for your help. Thank...


Update Solution

Here's what I do based on Daniel Alexiuc's suggestion :

Application.java

public class Application extends Controller {

    public static void index() {
        render();
    }

    public static void getData() {
        /** 
         *  Do some process before call getDataName method 
         *  and send the result of the process as the parameter.
         *  That why I do this way that look like redundancy process.
        **/
        getDataName(null);
    }

    public static void getDataName(String name) {
        // Didn't use ternary operation here because will become error 'not a statement'
        if(name == null)
            renderText("Without Parameter");
        else
            renderText("With Parameter name = " + name);
    }

}

<strong> routes

GET     /                                       Application.index
GET     /default                                Application.getData
GET     /dataString                             Application.getDataName

Solution Update (26/07)

Application.java

public class Application extends Controller {

    public static void index() {
        render();
    }

    public static void getData(String name, Integer age) {
        if (name == null && age == null) {
            renderText("Data null");
        } else if (name != null && age == null) {
            renderText("Name: " + name);
        } else if (name != null && age != null) {
            renderText("Name: " + name + "\n" + "Age: " + age);
        }
    }
}

<strong> routes

GET     /data                                   Application.getData
GET     /data/{name}                            Application.getData
GET     /data/{name}/{age}                      Application.getData

And to call:

 1. http://localhost:9000/data                -> Display "Data null"
 2. http://localhost:9000/data/Crazenezz      -> Display "Name: Crazenezz"
 3. http://localhost:9000/data/Crazenezz/24   -> Display "Name: Crazenezz
                                                          Age: 24"
+5
source share
2 answers

I don’t think you can overload it in the routes file as you want.

but you can just do something like this:

public static void getData(String name) {
  if (name == null) {
    renderText("Without Parameter");
  } else {
    renderText("With Parameter name = " + name);
  }
}
+2

, , , Crazenezz, :

GET     /terms.json                 controllers.exposedjson.TermServiceJSON.findTermsByQuery(langId:Long ?= null)

... , , , , null . , ( ), JSON, .

GET     /terms.json                 controllers.exposedjson.TermServiceJSON.findTermsByQuery(langId:String ?= "")


@Transactional(readOnly=true)
public static Result findTermsByQuery(String languageId) {
    Result result;
    if (languageId == "")
        result = ok(toJson(TermService.getTerms()));
    else
        result = ok(toJson(TermService.findTermsByLanguageId(Long.parseLong(languageId.trim()))));
    return result;
}

, , .

+1

All Articles