Usage

Previous

In the previous steps, you created a simple REST client and made it dynamically configurable.

Within an HST component, the REST client can be retrieved through the ComponentManager.

For example, you would use the following code to retrieve the GoGreenClient:

final GoGreenClient client = HstServices.getComponentManager().getComponent(GoGreenClient.class);

Below is a simple HST component which uses the GoGreenClient to retrieve the top products from the GoGreen RESTful service and makes them available to the rendering engine:

package org.example.components;

import java.util.List;

import org.example.jaxb.Products;
import org.example.jaxb.Products.Product;
import org.example.rest.client.GoGreenClient;
import org.hippoecm.hst.component.support.bean.BaseHstComponent;
import org.hippoecm.hst.core.component.HstComponentException;
import org.hippoecm.hst.core.component.HstRequest;
import org.hippoecm.hst.core.component.HstResponse;
import org.hippoecm.hst.site.HstServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TopProducts extends BaseHstComponent {

    public static final Logger log = LoggerFactory.getLogger(TopProducts.class);

    @Override
    public void doBeforeRender(final HstRequest request, final HstResponse response) throws HstComponentException {
        final GoGreenClient client = HstServices.getComponentManager().getComponent(GoGreenClient.class);
        final Products topProducts = client.getTopTenProducts();
        final List<Product> products = topProducts.getProduct();
        request.setAttribute("products", products);
    }
}

A Freemarker template can be used to render the products:

<#if products??>
  <ul>
    <#list products as product>
      <li>${product.localizedName}</li>
    </#list>
  </ul>
</#if>

Previous

Did you find this page helpful?
How could this documentation serve you better?
On this page
    Did you find this page helpful?
    How could this documentation serve you better?

    We rely on cookies

    to optimize our communication and to enhance your customer experience. By clicking on the Accept and Close button, you agree to the collection of cookies. You can also adjust your preferences by clicking on Manage Preferences. For more information please see our Privacy policy.

    Manage cookies
    Accept & close

    Cookies preferences

    Accept & close
    Back