Bird
0
0

What is wrong with this code snippet that tries to pretty-print JSON?

medium📝 Debug Q14 of 15
Spring Boot - Request and Response Handling
What is wrong with this code snippet that tries to pretty-print JSON?
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.PRETTY_PRINT);
String json = mapper.writeValueAsString(user);
ASerializationFeature.PRETTY_PRINT does not exist; should use INDENT_OUTPUT
BwriteValueAsString cannot produce pretty JSON
CObjectMapper must be configured with a custom serializer
DPretty print requires calling writeValue() instead
Step-by-Step Solution
Solution:
  1. Step 1: Check correct SerializationFeature for pretty print

    The correct feature name is INDENT_OUTPUT, not PRETTY_PRINT.
  2. Step 2: Verify usage of writeValueAsString

    writeValueAsString supports pretty printing if INDENT_OUTPUT is enabled.
  3. Final Answer:

    SerializationFeature.PRETTY_PRINT does not exist; should use INDENT_OUTPUT -> Option A
  4. Quick Check:

    Use INDENT_OUTPUT for pretty print [OK]
Quick Trick: Use SerializationFeature.INDENT_OUTPUT for pretty JSON [OK]
Common Mistakes:
  • Using wrong feature name PRETTY_PRINT
  • Thinking writeValueAsString can't pretty print
  • Believing custom serializers are always needed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes