Fórumok

Calling External Rest API

Max Hamulyak, módosítva 9 év-val korábban

Calling External Rest API

Junior Member Bejegyzések: 29 Csatlakozás dátuma: 2014.10.13. Legújabb bejegyzések
Hello,

I was wondering how I can call an external API from a Liferay Portlet and render the given response. For example in the development environment Liferay is based on localhost:8080 and the external API on localhost:8082/API. Both JSON Responses and XML Responses are possible with the API.

Thank You
Max
thumbnail
David H Nebinger, módosítva 9 év-val korábban

RE: Calling External Rest API (Válasz)

Liferay Legend Bejegyzések: 14916 Csatlakozás dátuma: 2006.09.02. Legújabb bejegyzések
As long as it is data retrieval only, you can put it directly into the render phase, i.e. in view.jsp. Personally I'd go w/ Spring's RestTemplate rather than trying to code your own stuff, but of course it's up to you.

End of the day, there's nothing magical about invoking a service, you can just use any old web reference for the implementation.
Max Hamulyak, módosítva 9 év-val korábban

RE: Calling External Rest API

Junior Member Bejegyzések: 29 Csatlakozás dátuma: 2014.10.13. Legújabb bejegyzések
Thanks for your reply, sadly I also have to make PUT/POST request to the external API. Do the same principles apply here as well?

Cheers
Max
thumbnail
David H Nebinger, módosítva 9 év-val korábban

RE: Calling External Rest API (Válasz)

Liferay Legend Bejegyzések: 14916 Csatlakozás dátuma: 2006.09.02. Legújabb bejegyzések
For put/post, you should only be doing those during the action phase. If you tried to do that during render, whenever the portal re-renders the page the put/post would be re-invoked.
Max Hamulyak, módosítva 9 év-val korábban

RE: Calling External Rest API

Junior Member Bejegyzések: 29 Csatlakozás dátuma: 2014.10.13. Legújabb bejegyzések
I think I understand it now, however I am still unclear as how to achieve this in a portlet. I keep finding stuff on the internet about services and defining all the routes in advance and so on, but since I am new to portlet development and even Java (Only have C# and ASP.NET/MVC experience) so I am unclear as where to start building this functionality. Currently I have in the development environment running Liferay on port localhost:8080 and a backend is a grails application on localhost:8082/grailsapp/api/.... Some of which will give me back XML and some of which will give me back JSON via the get. In the same way I will have to use PUT/POST request to the different action of the grails controller. It would really help if you could give me some pointers (maybe sample code or good blogpost) on where to start building this. See also https://www.liferay.com/community/forums/-/message_boards/message/43866795 for some of the functionality I am trying to create.

Cheers
Max
Max Hamulyak, módosítva 9 év-val korábban

RE: Calling External Rest API

Junior Member Bejegyzések: 29 Csatlakozás dátuma: 2014.10.13. Legújabb bejegyzések
David H Nebinger:
For put/post, you should only be doing those during the action phase. If you tried to do that during render, whenever the portal re-renders the page the put/post would be re-invoked.



Thank You, RestTemplate seems to work only need to figure out what kind of json/xml parser to use so I can handle this data. Thank You for your time

Regards
Max