Last active
September 12, 2020 06:35
-
-
Save viveknaskar/8daa9dc0730f56b45cc173e227adb211 to your computer and use it in GitHub Desktop.
HTTP POST and HTTP GET using RestTemplate
This file contains 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
// For GET request | |
if (marks != null && roll != null && !roll.isEmpty()) { | |
RestTemplate restTemplate = new RestTemplate(); | |
String url = PropsUtil.get("some.server") + "/mark/v1/" + roll; | |
ResponseEntity<String> result = restTemplate.getForEntity(url, String.class); | |
String data = result.getBody(); | |
if (data != null) { | |
JSONObject json = JSONFactoryUtil.createJSONObject(data); | |
if( json.has("passed")){ | |
renderRequest.setAttribute("passed", json.getBoolean("passed")); | |
}else{ | |
renderRequest.setAttribute("passed", false); | |
} | |
} | |
} | |
// For POST request | |
if (marks != null && roll != null && !roll.isEmpty()) { | |
RestTemplate restTemplate = new RestTemplate(); | |
String url = PropsUtil.get("some.server") + "/mark/v1"; | |
JSONObject request = JSONFactoryUtil.createJSONObject(); | |
request.put("roll", roll); | |
HttpHeaders headers = new HttpHeaders(); | |
headers.setContentType(MediaType.APPLICATION_JSON); | |
HttpEntity<String> entity = new HttpEntity(request.toString(), headers); | |
ResponseEntity<String> result = restTemplate.postForEntity(url, entity, String.class); | |
String data = result.getBody(); | |
if (data != null) { | |
JSONObject json = JSONFactoryUtil.createJSONObject(data); | |
if( json.has("passed")){ | |
renderRequest.setAttribute("passed", json.getBoolean("passed")); | |
}else{ | |
renderRequest.setAttribute("passed", false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment