title: How can I get the json file from ResponseEntity?
body: <p>I have my web service consuming another web service. in my web service I modify some fields of the json. then in postman it is consumed ok.
  Now I need to convert the ResponseEntity to a .json file to save it to a folder on my computer, but the attempt has failed.</p>
  <p>Any ideas?</p>
  <p>my controller current:</p>
  <pre><code>@Controller
  public class SiviccController {
  
      private final Logger log = LoggerFactory.getLogger(SiviccController.class);
      private String url = &quot;http://localhost:9090/url_1&quot;;
  
      @GetMapping(&quot;/url_2&quot;)
      @ResponseBody
      public Body sivicc() {
          RestTemplate restTemplate = new RestTemplate(Factory.timeoutHttp());
          try {
              return restTemplate.getForObject(url, Body.class);
          } catch (Exception e) {
              Body body = new Body();
              body.setIserror(true);
              log.info(e.getMessage());
              return body;
          }
      }
  }
  </code></pre>
  <p>Matheus Cirillo works perfectly with the following code:</p>
  <pre><code>@Controller
  public class SiviccController {
  
      private final Logger log = LoggerFactory.getLogger(SiviccController.class);
      private String url = &quot;http://localhost:9090/url_1&quot;;
  
      @GetMapping(&quot;/url_2&quot;)
      @ResponseBody
      public Body sivicc() {
          RestTemplate restTemplate = new RestTemplate(Factory.timeoutHttp());
          try {
              String response = restTemplate.getForObject(url, String.class);
              BufferedWriter writer = new BufferedWriter(new FileWriter(&quot;my-file.json&quot;, true));
              writer.append(response);
              writer.close();
              return restTemplate.getForObject(url, Body.class);
          } catch (Exception e) {
              Body body = new Body();
              body.setIserror(true);
              log.info(e.getMessage());
              return body;
          }
      }
  }
  </code></pre>
  <p>However, the web service that you were consuming before now generates a token. That's why I change getForObject to exchange and I consume it well, but I apply the same logic to save the json in a folder on my computer, but it generates a failure in two lines:</p>
  <pre><code>@Controller
  public class SiviccController {
  
      private final Logger log = LoggerFactory.getLogger(SiviccController.class);
      private String url = &quot;http://localhost:9090/url_1&quot;;
  
      @GetMapping(&quot;/url_2&quot;)
      @ResponseBody
      public Body sivicc() {
          RestTemplate restTemplate = new RestTemplate();
          HttpHeaders headers = new HttpHeaders();
          headers.setContentType(MediaType.APPLICATION_JSON);
          headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
          headers.set(&quot;Authorization&quot;, &quot;bearer 8P7oM_ZDhB3TYolVkB1MLtM734DrrUSMVEFuiy3u&quot;);
          HttpEntity request = new HttpEntity(headers);               
          headers.add(&quot;User-Agent&quot;, &quot;Spring's RestTemplate&quot; );
          ResponseEntity&lt;String&gt; response = restTemplate.exchange(
                  url,
                  HttpMethod.GET,
                  request,
                  //Body.class,
                  String.class,
                  1
          );
    
          try {   
              BufferedWriter writer = new BufferedWriter(new FileWriter(&quot;my-file.json&quot;, true));
              writer.append(response);//Error: The method append(CharSequence) in the type Writer is not applicable for the arguments (ResponseEntity&lt;String&gt;)
              writer.close();
              return response.getBody();//Error: Type mismatch: cannot convert from String to Body
              
          } catch (Exception e) {
              Body body = new Body();
              body.setIserror(true);
              log.info(e.getMessage());
              return body;
          }
      }
  }
  </code></pre>