Overload method in Application.java Play Framework

java, overloading, playframework

Solution

I don't think it is possible to overload it in the routes file like you want.

but you could just do something like this:

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

Problem

How to set Overloading method in Application.java on Play Project? Here is some example what I'm currently doing: 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 ``` # 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} ``` The Test: - Call `getData` without parameter `http://localhost:9000/data` - Call `getData` with parameter `http://localhost:9000/data?name=test` The Result: - With Parameter name = null - With Parameter name = test What I want for the result: - Without Parameter - With Parameter name = test I'm very appreciated with your helps. Thank you... Update Solution Here is what I'm doing based on Daniel Alexiuc 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'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); } } ``` routes ``` GET / Application.index GET /default Application.getData GET /dataString Application.getDataName ``` Update Solution (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); } } } ``` routes ``` GET /data Application.getData GET /data/{name} Application.getData GET /data/{name}/{age} Application.getData ``` And for the 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" ```

Original source