Reputation: 23
I have one cloud event in my code which contains byte data, now I want to send this cloud event to a URL. I've tried Feign client and rest template but not able to send the cloud event. Can anyone help me here, how can I do this? In Quarkus I used @RegisterRestClien and @RestClient Combination to send it and it works perfectly but how can I send it in spring boot?
Upvotes: 1
Views: 310
Reputation: 23
Earlier I was trying to send a CloudEvent object directly which I guess not supported by spring boot (I also rovided all necessary dependencies in pom.xml) but it was not working. But cloud event is only a json data with some specific field like specVersion, type etc. in it's header, So i created a json structure where all these fields (ce-specVersion, ce-type etc) I put in header and then send this using restTemplate and it worked like below -
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import java.nio.charset.StandardCharsets;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("ce-specversion", "1.0");
// Set Remaining fields
byte[] bytesData =
JSON_DATA_IN_FORM_OF_STRING.getBytes(StandardCharsets.UTF_8);
HttpEntity<byte[]> httpEntity = new HttpEntity<>(bytesData, headers);
restTemplate.postForLocation(sinkUrl, httpEntity);
But remember one thing if you are sending any field in header then it should contain 'ce-' as prefix (Apart from the Data that we are sending in HttpEntity) like in my case there was one key 'partitionKey' so I send it as 'ce-partitionKey'.
Upvotes: 0