Import Data from REST API

Overview

Fetches JSON data from an external REST API using pfx-rest:get and loads it into Pricefx using pfx-api:loaddata. Uses an OAuth2 connection for authentication.

Prerequisites

Define the REST connection in connections/external-api.json (see Connection Examples > OAuth2 REST Connection Setup).

Mapper: mappers/product-api-mapper.xml

XML
<mappers>
    <loadMapper id="productApiMapper">
        <body in="productCode" out="sku"/>
        <body in="productName" out="label"/>
        <body in="currencyCode" out="currency"/>
        <groovy expression="body.unitPrice?.toBigDecimal()" out="attribute1"/>
        <constant expression="ACTIVE" out="attribute2"/>
    </loadMapper>
</mappers>

Route: routes/import-from-rest.xml

XML
<routes xmlns="http://camel.apache.org/schema/spring">
    <route id="importProductsFromApi">
        <from uri="scheduler://apiImport?delay={{api.poll.interval.ms}}"/>
        <to uri="pfx-rest:get?uri=/v1/products&amp;connection=externalApi"/>
        <!-- REST response body is a JSON array — convert to List of Maps -->
        <unmarshal>
            <json library="Jackson" useList="true"/>
        </unmarshal>
        <to uri="pfx-api:loaddata?objectType=P&amp;mapper=productApiMapper"/>
        <onCompletion onCompleteOnly="true">
            <to uri="pfx-api:internalCopy?label=Product"/>
        </onCompletion>
    </route>
</routes>

Properties:

api.poll.interval.ms=3600000

How It Works

  1. pfx-rest:get calls GET /v1/products on the externalApi connection

  2. The response body is a JSON string — unmarshal with useList=true converts it to List<Map>

  3. pfx-api:loaddata sends rows to Pricefx using the mapper

  4. pfx-api:internalCopy publishes the loaded data to make it visible in Pricefx

Common Pitfalls

  • pfx-rest:get returns the raw response body as a String — always unmarshal JSON before passing to pfx-api:loaddata

  • Use useList=true in Jackson unmarshal when the response is a JSON array

  • If the API returns {"items": [...]} (wrapped object), extract with <setBody><simple>${body[items]}</simple></setBody> before loaddata