Example with Simple JSON REST API

Overview

This example demonstrates how to configure a ResourceResolver bean to connect to a simple JSON REST API. The configuration covers connection details, RestTemplate setup, ResourceLinkResolver customization, and cache control.

You can apply the following ResourceResolver Spring bean configuration at the repository path /hippo:configuration/hippo:modules/crispregistry/hippo:moduleconfig/crisp:resourceresolvercontainer/demoProductCatalogs/@crisp:beandefinition for the "demoProductCatalogs" resource space.

ResourceResolver Configuration

Assume your backend REST API exposes product data at URLs matching http://localhost:8080/example-commerce/api/v1/products/* and returns JSON. To integrate this API, configure a ResourceResolver using SimpleJacksonRestTemplateResourceResolver. This setup allows the application and templates to access product data as Resource objects.

Refer to the JavaDocs for detailed API documentation.

brXM 16:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> <bean parent="abstractCrispSimpleJacksonRestTemplateResourceResolver" class="org.onehippo.cms7.crisp.core.resource.jackson.SimpleJacksonRestTemplateResourceResolver"> <property name="cacheEnabled" value="${cache.enabled}" /> <property name="baseUri" value="${example.commerce.api.base.uri}" /> <property name="restTemplate"> <bean class="org.springframework.web.client.RestTemplate"> <property name="requestFactory" ref="org.springframework.http.client.ClientHttpRequestFactory" /> </bean> </property> <property name="resourceLinkResolver"> <bean class="org.onehippo.cms7.crisp.core.resource.FreemarkerTemplateResourceLinkResolver"> <property name="templateSource"> <value>http://www.example.com/products/${(preview == "true")?then("staging", "current")}/sku/${resource.valueMap['SKU']!"unknown"}/overview.html</value> </property> </bean> </property> <property name="resourceDataCache"> <bean class="org.onehippo.cms7.crisp.core.resource.SpringResourceDataCache"> <constructor-arg> <bean class="org.springframework.cache.caffeine.CaffeineCache" factory-bean="caffeineCacheInstanceFactory" factory-method="createSpringCacheInstance"> <constructor-arg value="demomarketoCache" /> <constructor-arg value="initialCapacity=10,maximumSize=1000,expireAfterWrite=100s,expireAfterAccess=100s" /> </bean> </constructor-arg> </bean> </property> </bean> </beans>

brXM 14 & 15:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> <bean parent="abstractCrispSimpleJacksonRestTemplateResourceResolver" class="org.onehippo.cms7.crisp.core.resource.jackson.SimpleJacksonRestTemplateResourceResolver"> <property name="cacheEnabled" value="${cache.enabled}" /> <property name="baseUri" value="${example.commerce.api.base.uri}" /> <property name="restTemplate"> <bean class="org.springframework.web.client.RestTemplate"> <property name="requestFactory" ref="org.springframework.http.client.ClientHttpRequestFactory" /> </bean> </property> <property name="resourceLinkResolver"> <bean class="org.onehippo.cms7.crisp.core.resource.FreemarkerTemplateResourceLinkResolver"> <property name="templateSource"> <value>http://www.example.com/products/${(preview == "true")?then("staging", "current")}/sku/${resource.valueMap['SKU']!"unknown"}/overview.html</value> </property> </bean> </property> <property name="resourceDataCache"> <bean class="org.onehippo.cms7.crisp.core.resource.SpringResourceDataCache"> <constructor-arg> <bean class="org.springframework.cache.ehcache.EhCacheCache"> <constructor-arg> <bean parent="abstractCrispResourceEhCache"> <property name="cacheName" value="demoProductCatalogsCache" /> <property name="maxEntriesLocalHeap" value="1000" /> <property name="maxEntriesLocalDisk" value="0" /> <property name="timeToLiveSeconds" value="60" /> <property name="timeToIdleSeconds" value="60" /> </bean> </constructor-arg> </bean> </constructor-arg> </bean> </property> </bean> </beans>

The following properties are referenced in the bean configuration. Set these as pairs of crisp:propnames and crisp:propvalues at the /hippo:configuration/hippo:modules/crispregistry/hippo:moduleconfig/crisp:resourceresolvercontainer/demoProductCatalogs node for the "demoProductCatalogs" resource space.

Property NameExample Value
cache.enabledtrue
example.commerce.api.base.urihttp://localhost:8080/example-commerce/api/v1

Property details:

  • cacheEnabled: Enables or disables caching at the ResourceResolver level.
  • baseUri: Sets the base URI for the backend REST API. The ResourceServiceBroker uses this base URI and appends relative resource paths when invoking the backend service.
  • restTemplate: Configures the Spring Framework RestTemplate instance. In this example, no authentication is required, so a default RestTemplate is used. The org.springframework.http.client.ClientHttpRequestFactory is the default request factory in the CRISP module.
  • resourceLinkResolver: Configures a custom ResourceLinkResolver for this ResourceResolver. External content or data does not have automatically generated URI links, so you must define how to generate them. The CRISP module provides a generic Freemarker template-based resolver (org.onehippo.cms7.crisp.core.resource.FreemarkerTemplateResourceLinkResolver) for this purpose.
  • resourceDataCache: Specifies a ResourceDataCache instance for this ResourceResolver. If set and the resource is cacheable, the ResourceServiceBroker caches resource data in this cache. If not set, but the resource is cacheable, the broker uses a global cache by default.

Configuring Basic Authentication

If your backend REST API only supports Basic Authentication, you can configure the ResourceResolver to send the required headers. Spring Framework provides org.springframework.http.client.support.BasicAuthorizationInterceptor for this purpose. Add it to the restTemplate bean as shown below:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> <bean parent="abstractCrispSimpleJacksonRestTemplateResourceResolver" class="org.onehippo.cms7.crisp.core.resource.jackson.SimpleJacksonRestTemplateResourceResolver"> <!-- SNIP --> <property name="restTemplate"> <bean class="org.springframework.web.client.RestTemplate"> <!-- SNIP --> <!-- Add BasicAuthorizationInterceptor to enable Basic Authentication --> <property name="interceptors"> <list> <bean class="org.springframework.http.client.support.BasicAuthorizationInterceptor"> <constructor-arg value="${basic.auth.username}" /> <constructor-arg value="${basic.auth.password}" /> </bean> </list> </property> </bean> </property> <!-- SNIP --> </bean> </beans>

This configuration expects two string properties, basic.auth.username and basic.auth.password, to be set as pairs of crisp:propnames and crisp:propvalues at /hippo:configuration/hippo:modules/crispregistry/hippo:moduleconfig/crisp:resourceresolvercontainer/demoProductCatalogs for the "demoProductCatalogs" resource space.

Note: The interceptors property of RestTemplate is a list of org.springframework.http.client.ClientHttpRequestInterceptor instances. The BasicAuthorizationInterceptor adds Basic Authentication headers to each request. For custom behaviors, such as adding other headers to every request, implement your own ClientHttpRequestInterceptor. Refer to the JavaDocs and source code for implementation details.

Share Feedback
Page: /build/crisp-api/configuration/example-with-simple-json-rest-api
Section: Build
Category *