How to post json to Cumulocity using RestController?


I have a json document, that either represent an alarm, measurement or event.
This json is received in a microservice and I want to post this to Cumulocity.
How can I post this?

Two approaches come to my mind:

  1. Either turn the json in a specific REST representation? e.g. MeasurementRepresentation
    Is there a sample available?
  2. Use the RestController (com.cumulocity.sdk.client.RestConnector)?
    What is the appropriate post methods? postSteam

Hi Christof,
I would go with the first approach for now.

There is useful library called ObjectMapper with which you can read json from multiple sources (String, InputStream, etc.) and try to map it into e.g. MeasurementRepresentation.

A small code snippet:

import com.fasterxml.jackson.databind.ObjectMapper;
// "json" in the next line could be a String, InputStream, File, Reader or alike
MeasurementRepresentation mr = objectMapper.readValue(json, MeasurementRepresentation.class);

Like this you could validate which mapping works, if it is an alarm, event or measurement.
The readValue method would throw an exception or return null in case the mapping is not successful.

Regards, Kai

1 Like