Метод перегрузки в Application.java Play Framework

Как установить метод перегрузки в Application.java в Play Project?

Вот пример того, что я сейчас делаю:

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);
    }
}

маршруты

# 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}

Тест:

  1. Вызов getDataбез параметраhttp://localhost:9000/data
  2. Вызов getDataс параметромhttp://localhost:9000/data?name=test

Результат:

  1. С именем параметра = null
  2. С именем параметра = test

Что я хочу от результата:

  1. Без параметра
  2. С именем параметра = test

Я очень ценю вашу помощь. Спасибо...


Обновление решения

Вот что я делаю на основе Даниэля Алексюка предложения:

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's 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);
    }

}

маршрутов

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

Обновление решения (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);
        }
    }
}

маршруты

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

И для вызова:

 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
задан Community 23 May 2017 в 12:22
поделиться