Reputation: 7919
I am trying to use JavaWs in play 2.4 for that I have added javaWs to dependence
then added @Inject static WSClient ws;
to my controller.
But the problem is that if I do
Logger.info(ws);
inside my controller method its printing null in console. I cant seem to figure out the issue because if it is the dependency issue then WsClient
wont be imported.
Is there anything I am missing?
Upvotes: 0
Views: 197
Reputation: 1963
According to the Guice documentation using of static injection is not recommended and it should be a special reason to use it. In order to use static injection you should add to the module configure the following:
requestStaticInjection(WSClient.class);
But instead I suggest just to remove static from WSClient ws.
It is even better to use Provider injection instead. In this case WSClient will be instantiated only when it is really needed. To inject :
@Inject
private Provider<WSClient> wsProvider;
To create WSClient:
WSRequest request = wsProvider.get().url("url");
Besides, if you mean to unit test the controller, direct injecting of WSClient may be problematic.
You can read more in this post about injection in Play; in spite the post refers to Play 2.5, all the ideas are similar for 2.4 as well.
Upvotes: 1