Last active
July 21, 2022 04:08
-
-
Save NahianAhmed/cfc1f77c8f01c35b995a4bd1d6c1e7c9 to your computer and use it in GitHub Desktop.
send and get JSON data using Rest Template api call
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@PostMapping(value = MY_URL, produces = MediaType.APPLICATION_JSON_VALUE) | |
@ApiResponse(code = 200, message = "OK") | |
public void getData(@RequestBody String data) { | |
String val = data; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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