title: How to set the response content type dynamically in Spring Rest Controller?
body: <p>I have implemented an API which will return both JSON and XML as per the client's request (i.e., Accept header in the request). Its working as expected.</p>
  
  <p>The problem here is: even though user sends Accept header, on some special cases (i.e., business logic) we need to send the response content type dynamically.
  I have tried to implement it using HttpHeaders but its not working.</p>
  
  <pre><code>@GetMapping
  public ResponseEntity&lt;Response&gt; getQueries(){
  ...............
  
      HttpHeaders headers = new HttpHeaders();
      headers.setContentType(MediaType.APPLICATION_JSON);
      //headers.add("Content-Type", "application/json");  
      return new ResponseEntity&lt;Response&gt;(response, headers, HttpStatus.OK);
  }
  </code></pre>
  
  <p>I am geeting HTTP status as 200 but response as error i.e., </p>
  
  <pre><code>Unexpected '&lt;'
  </code></pre>
  
  <p>How to fix this ?</p>