Skip to content

Instantly share code, notes, and snippets.

@NahianAhmed
Last active July 21, 2022 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NahianAhmed/cfc1f77c8f01c35b995a4bd1d6c1e7c9 to your computer and use it in GitHub Desktop.
Save NahianAhmed/cfc1f77c8f01c35b995a4bd1d6c1e7c9 to your computer and use it in GitHub Desktop.
send and get JSON data using Rest Template api call
@PostMapping(value = MY_URL, produces = MediaType.APPLICATION_JSON_VALUE)
@ApiResponse(code = 200, message = "OK")
public void getData(@RequestBody String data) {
String val = data;
}
public void sendData() {
try {
HttpHeaders headers = new HttpHeads();
JSONObject requestBody = new JSONObject();
requestBody.put("data", "Hello world");
HttpEntity<String> requestEntity = new HttpEntity<>(requestBody.toString(), headers);
String url = "your url";
restTemplate.exchange(url , HttpMethod.POST, requestEntity, String.class);
} catch (RestClientException ex) {
log.error("Error while sending data : " + ex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment