'Post JSON Object as response to url
Good day everyone please I am supposed to send back a JSON object as a response from my service using RestTemplate but I am confused about where to add the JSON data with restTemplate.exchange() method.
Please see the code below. Thank you in advance.
JSONObject json= new JSONObject();
json.put("responseCode", 200);
json.put("responseMessage", "Transaction Successfully proceed");
json.put("responseData", transactionDetail.toJson());
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Object> entity = new HttpEntity<>(headers);
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
jsonHttpMessageConverter.getObjectMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
restTemplate.getMessageConverters().add(jsonHttpMessageConverter);
ResponseEntity<String> resultFromVGIL = restTemplate.exchange(bitbackCallBackUrl, HttpMethod.POST, entity, String.class);
Solution 1:[1]
I was supposed to add the json object in the HttpEntity
HttpEntity<Object> entity = new HttpEntity<>(json.toString(), headers);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | BALDE |