Retrieving port number in Play Framework 2 app

后端 未结 3 2207
心在旅途
心在旅途 2021-01-05 09:49

The question is: how do I retrieve the port number that play is listening on, no matter how it was defined (configuration, command line argument, or not at all).

Thi

3条回答
  •  死守一世寂寞
    2021-01-05 10:16

    I just found a way to get the port no matter how the port is specified. Use the reverse route to get an Call object, and the use the absoluteUrl() method to get the absolute url of your action, which contains port. Code snippet:

    public class RouteController extends Controller {
    public  Promise getPortAction() {
        Call call = com.mycom.routes.RouteController.getPortAction();
        String absoluteUrl = call.absoluteURL(request());
        //Here absoluteUrl would be like http://localhost:9002/...., 9002 would be the port.
        ...
    } }
    

提交回复
热议问题