My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians


marți, 6 octombrie 2015

JAX-RS consume a RESTful web service from JSF

JavaEE 7 Client API for JAX-RS API is useful to access the RESTful web services. Basic steps:

  • Get the instance of class (entry point for invoking RESTful web services).
  • Create an instance of using the instance of Client class (used to invoke a RESTful web service at some location or URI).
  • Populate the target with the required data (e.g. MIME type, post data, query parameters), and create a request of appropriate HTTP method type which would be an instance of
  • Obtain the response from the desired RESTful web service via object.

Let's suppose that we have the following JAX-RS resource:


public class HelloWorldResource {

 @Produces("text/plain") // default: */*
 public String helloWorld() {
  return "Hello, World!";

Configured as:


public class ApplicationConfig extends Application {

Supposing that our application is named JaxrsSimpleJSFClient_EE7, our resource is available at:


From a CDI managed bean we can access this resource like below:

public class ClientBean implements Serializable {

 private final Client jaxrsClient;
 // for simple demo, URL is hard-coded
 private final String jaxrsResource = "http://localhost:8080/JaxrsSimpleJSFClient_EE7/webresources/helloworld/";
 private String hello;

 public ClientBean() {
  // get the instance of client which will be entry point to invoking services
  jaxrsClient = ClientBuilder.newClient();

 public void sayHelloWorldAction() {
  // targeting the JAX-RS serivce we want to invoke by capturing it in WebTarget instance
  WebTarget webTarget =;

  // build the request (e.g. a GET request)
  Invocation invocation = webTarget.request("text/plain").buildGet();

  // invoke the request
  Response response = invocation.invoke();
  // set the response in the bean property
  this.hello = response.readEntity(String.class);

 public String getHello() {
  return hello;

 public void setHello(String hello) {
  this.hello = hello;

A simple JSF page for test:

 <h:commandButton value="JAX-RS Hello World!" action="#{clientBean.sayHelloWorldAction()}"/>

The complete application is available here.

Read also:

Niciun comentariu :

Trimiteți un comentariu


Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors