默认情况下使用 Spring Boot 控制器返回缩小的JSON,但是对于某些请求,如果传递了特殊的请求参数,响应应该是相当格式化的。例如:
http://test.com/health =>返回缩小的JSON
http://test.com/health?pretty =>返回相当格式化的JSON
如何扩展请求映射处理程序/控制器以选择性地格式化输出并保持生成的mime类型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);
}