1

Using Spring Boot 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:

http://test.com/health => returns minified JSON

http://test.com/health?pretty => returns pretty formatted JSON

How to extend the request mapping handler/controller to optionally format the output and keep the produced mime type application/json?

@RequestMapping(
            value = {"/health", "/"},
            produces = APPLICATION_JSON_VALUE)
public ResponseEntity<?> checkHealth(@RequestParam(required = false, value = "pretty") String prettyapplicationInfo) {
    final Map<String, Object> response = getResponse();

    if (pretty != null) {
        // so, how to format the response, if "pretty" property is specified? 
    }

    return new ResponseEntity<>(response, HttpStatus.OK);

}
radistao
  • 14,889
  • 11
  • 66
  • 92
  • maybe this will help : https://stackoverflow.com/questions/36669172/how-to-enable-dynamic-pretty-print-of-json-based-on-http-request-header-in-sprin/36669727#36669727 – Azocan Kara Dec 08 '17 at 17:06
  • really, my bad - was not able to find it. But i like this answer more: https://stackoverflow.com/a/46227319/907576 – radistao Dec 10 '17 at 19:26

0 Answers0