2

I have a legacy application written in JSF 1 but I have this requirement to edit the code and call a rest service in my managed bean prior to displaying the user interface.

I am just not too sure how to implement this? Anyone have idea over this?

Mark Estrada
  • 9,013
  • 37
  • 119
  • 186

1 Answers1

1

Actually it isn't an good idea to perform this from @ManagedBean, you in this way mix here presentation and logic. @ManagedBean related more to the presentation layer. In any way you should perform HTTP POST or GET request to your service, get the answer and parse it to get the required values. See this tutorial for complete example: http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/

Anatoly
  • 5,056
  • 9
  • 62
  • 136
  • Thanks. I am actually looking for a solution to this. I am just not sure what to do. So given the link you have sent, what do I need to do to call the REST service before my user interface is displayed? The samples in that link are java classes with main method. I am not sure how to integrate that in my JSF applications though. Can you elaborate more. By the way this is legacy JSF 1.2 so no annotation is available. – Mark Estrada Nov 12 '14 at 07:08
  • you need to call it from @PostConstruct method : https://docs.oracle.com/javaee/5/api/javax/annotation/PostConstruct.html. Main class provided for demonstration purposes only, you can throw it away, change modifier of sendGet and sendPost from private to public and even make them static and call to them from your bean. For example HttpURLConnectionExample.sendGet(); – Anatoly Nov 12 '14 at 07:30