# How to post json to Cumulocity using RestController?

**URL:** <https://community.cumulocity.com/t/how-to-post-json-to-cumulocity-using-restcontroller/5312>\
**Category:** Forum\
**Tags:** cumulocity\
**Created:** [August 5, 2022, 7:22am UTC](https://community.cumulocity.com/t/how-to-post-json-to-cumulocity-using-restcontroller/5312 "2022-08-05T07:22:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christof\_Strack](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/christof_strack/32/30066_2.png) [@Christof\_Strack](https://community.cumulocity.com/u/Christof_Strack)\
**Post date:** [August 5, 2022, 7:22am UTC](https://community.cumulocity.com/t/how-to-post-json-to-cumulocity-using-restcontroller/5312/1 "2022-08-05T07:22:59Z")

</div>

Hello,

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`

---

<div class="post-metadata">

**Author:** ![Kai\_Sieben](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/kai_sieben/32/5541_2.png) [@Kai\_Sieben](https://community.cumulocity.com/u/Kai_Sieben)\
**Post date:** [August 5, 2022, 8:16am UTC](https://community.cumulocity.com/t/how-to-post-json-to-cumulocity-using-restcontroller/5312/2 "2022-08-05T08:16:38Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/system/32/31990_2.png) [@system](https://community.cumulocity.com/u/system)\
**Post date:** [February 1, 2023, 8:17am UTC](https://community.cumulocity.com/t/how-to-post-json-to-cumulocity-using-restcontroller/5312/3 "2023-02-01T08:17:21Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
