If you are using jackson you can easily achieve this with configuring a SerializationFeature
in your ObjectMapper
:
com.fasterxml.jackson.databind.ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
mapper.writeValueAsString(<yourObject>);
Thats it.