title: How to make Spring Boot JSON output optionally formatted (pretty-printed)
body: <p>Using <em>Spring Boot</em> by default the controllers return minified JSON, but for some requests the response should be pretty formatted, if special request argument is passed. For example:</p>
  
  <p><a href="http://test.com/health" rel="nofollow noreferrer">http://test.com/health</a> => returns minified JSON</p>
  
  <p><a href="http://test.com/health?pretty" rel="nofollow noreferrer">http://test.com/health?pretty</a> => returns pretty formatted JSON</p>
  
  <p>How to extend the request mapping handler/controller to optionally format the output and keep the produced mime type <code>application/json</code>?</p>
  
  <pre class="lang-java prettyprint-override"><code>@RequestMapping(
              value = {"/health", "/"},
              produces = APPLICATION_JSON_VALUE)
  public ResponseEntity&lt;?&gt; checkHealth(@RequestParam(required = false, value = "pretty") String prettyapplicationInfo) {
      final Map&lt;String, Object&gt; response = getResponse();
  
      if (pretty != null) {
          // so, how to format the response, if "pretty" property is specified? 
      }
  
      return new ResponseEntity&lt;&gt;(response, HttpStatus.OK);
  
  }
  </code></pre>