0

Here , what I am trying to achieve is the following

  1. I have already got the body of the request to be made using the @RequestBody annotation .. which is in a String Format
  2. The body contains many nested parameters .. it is also pretty huge .. something like
{
  name : XYZ
  age : 21 
  education : {
    primaryschool : XYZ-ABC
    secondary school : ABC-123
    SubjectsChosen : {Science,Maths}
}}

this is still an oversimplification , the whole request body contains many nested parameters too

What I am trying to achieve is to wrap up the String body along with Headers into a HttpRequestEntity, that I can send along with PostForObject.

Is there a way to achieve this to directly convert the String .. without creating an explicit java class for each of the variables in the body as the body is quite huge ?

Moritz Ringler
  • 9,772
  • 9
  • 21
  • 34
Aditi
  • 157
  • 6
  • 1
    Take a look at this [link](https://stackoverflow.com/questions/4075991/post-request-via-resttemplate-in-json) I think this approach could work for your case – atakansaltik Jun 29 '22 at 13:56

1 Answers1

0
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
        
HttpEntity<String> entity = new HttpEntity<String>(requestJson, 
headers);
ResponseEntity<String> response = restTemplate.put(url, entity);
  • 1
    Hi @RITICK SAINI; welcome to stack overflow. Please read "[How to Answer](https://stackoverflow.com/help/how-to-answer)" and "[Explaining entirely code-based answers](https://meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers)". It helps more if you supply an explanation why this is the preferred solution and explain how it works. We want to educate, not just provide code. – Philip Adler Mar 21 '23 at 10:11