-1

My REST API response looks like this:

{"business_id": "vcNAWiLM4dR7D2nwwJ7nCA", "full_address": "4840 E Indian School Rd\nSte 101\nPhoenix, AZ 85018", "hours": {"Tuesday": {"close": "17:00", "open": "08:00"}, "Friday": {"close": "17:00", "open": "08:00"}, "Monday": {"close": "17:00", "open": "08:00"}, "Wednesday": {"close": "17:00", "open": "08:00"}, "Thursday": {"close": "17:00", "open": "08:00"}}, "open": true, "categories": ["Doctors", "Health & Medical"], "city": "Phoenix", "review_count": 7, "name": "Eric Goldberg, MD", "neighborhoods": [], "longitude": -111.98375799999999, "state": "AZ", "stars": 3.5, "latitude": 33.499313000000001, "attributes": {"By Appointment Only": true}, "type": "business"}

Is there any HTTP header or something generic like that which will beautify this JSON response?

It's an existing project so I can't really use any 3rd party library to change it in every service or controller layer. I'll need something that can be applicable for every response.

expected response:

{
  "business_id": "vcNAWiLM4dR7D2nwwJ7nCA",
  "full_address": "4840 E Indian School Rd\nSte 101\nPhoenix, AZ 85018",
  "hours": {
    "Tuesday": {
      "close": "17:00",
      "open": "08:00"
    },
    "Friday": {
      "close": "17:00",
      "open": "08:00"
    },
    "Monday": {
      "close": "17:00",
      "open": "08:00"
    },
    "Wednesday": {
      "close": "17:00",
      "open": "08:00"
    },
    "Thursday": {
      "close": "17:00",
      "open": "08:00"
    }
  },
  "open": true,
  "categories": [
    "Doctors",
    "Health & Medical"
  ],
  "city": "Phoenix",
  "review_count": 7,
  "name": "Eric Goldberg, MD",
  "neighborhoods": [],
  "longitude": -111.983758,
  "state": "AZ",
  "stars": 3.5,
  "latitude": 33.499313,
  "attributes": {
    "By Appointment Only": true
  },
  "type": "business"
}

I am looking for something like a "Content-type" header that decides the format of the response.

AkSh
  • 218
  • 1
  • 7

1 Answers1

0

Spring uses ObjectMapper class to serialize/deserialize json objects. You may override it by providing your custom configuration:

@Configuration
public class ObjectMapperConfig {
    @Bean
    public ObjectMapper objectMapper() {
        return new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
    }
}

This way any serialized json in your application(not only http responses) will be "pretty".

However if you want to prettify only http responses. You may override AbstractHttpMessageConverter class in a following way:

@Component
public class PrettyJsonMessageConverter extends AbstractHttpMessageConverter<Object> {

    @Autowired
    private ObjectMapper objectMapper;

    public PrettyJsonMessageConverter() {
        super(MediaType.APPLICATION_JSON);
    }

    @Override
    protected boolean supports(Class<?> clazz) {
        return true;
    }

    @Override
    protected Object readInternal(Class<? extends Object> clazz,
                                  HttpInputMessage inputMessage) throws HttpMessageNotReadableException, IOException {
        return objectMapper.readValue(inputMessage.getBody(), clazz);
    }

    @Override
    protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
        outputMessage.getBody().write(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsBytes(o));
    }
}
eparvan
  • 1,639
  • 1
  • 15
  • 26
  • Again, this will work for individual responses or endpoints. I am looking for something like a "Content-type" header that decides the format of the response. – AkSh Jun 09 '21 at 07:25
  • @ASAkram There are no headers that automatically change this. – Evert Jun 09 '21 at 07:34
  • @ASAkram once you configure objectMapper or AbstractHttpMessageConverter it will work for all responses no further action will be needed on your side. – eparvan Jun 09 '21 at 07:36