JSON Handling
Spring Boot uses Jackson for JSON by default on starter-web.
Introduction
Spring Boot uses Jackson for JSON by default on starter-web. Records and simple POJOs serialize through getters/components. Property names default to JavaBean style (amountCents stays amountCents).
Configure via application.yml (spring.jackson.*) or a Jackson2ObjectMapperBuilderCustomizer bean. Prefer explicit DTOs over hoping entities serialize cleanly.
Unknown properties: decide whether to fail (FAIL_ON_UNKNOWN_PROPERTIES) or ignore. Dates: prefer ISO-8601 Instant/OffsetDateTime over timestamps unless clients require otherwise.
Money stays integer minor units in JSON so clients never round floats.
Purpose of this lesson
Control Orders JSON field names, unknown-property policy, and time/money representation without leaking entities.
Understanding the topic
Jackson is part of your public API.
- Records: component names become JSON keys.
- @JsonIgnore / @JsonProperty: hide or rename carefully.
- Unknown props: failing closed catches client typos early.
- JavaTimeModule: Boot registers Java Time support by default in modern setups.
- Views: rarely needed if you have dedicated response records.
Step-by-step explanation
- Return
OrderViewand inspect JSON keys. - Add an unknown field in a POST body and observe default behavior.
- Configure fail-on-unknown if you want stricter contracts.
- Keep
amountCentsasint/long— neverdouble.
Informative example
Strict unknown properties for write DTOs.
@BeanJackson2ObjectMapperBuilderCustomizer ordersJackson() {return builder -> builder.featuresToEnable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);}
Request record the client posts:
record CreateOrderRequest(@NotBlank String sku,@Positive int qty) {}
Real-world use
When marketing adds couponCode to the app before the API accepts it, fail-on-unknown surfaces the mismatch in QA instead of silently dropping the field and surprising finance.
Security implications
JSON is a common injection and over-posting surface.
- Ignore or reject unknown properties intentionally.
- Never bind internal fields like
adminflags from client JSON. - Cap request body size at the server/proxy.
Best practices
- Dedicated request/response records per endpoint family.
- Contract tests for JSON snapshots on critical payloads.
- Document date formats once in the API guide.
Common mistakes
- Serializing entities with lazy fields.
- Using
float/doublefor money. - Renaming JSON fields casually and breaking mobile clients.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionWhat library does Boot use for JSON by default?+
Answer
2IntermediateQuestionWhy are records convenient for API payloads?+
Answer
3AdvancedQuestionHow do you avoid mass assignment?+
Answer
Hands-on exercise
Lab. Post a create body with an extra field. Enable fail-on-unknown and confirm 400. Remove the feature and note the silent drop — then decide which policy Orders wants.
Summary
JSON handling is API design. Next you use ResponseEntity when status and headers need precise control.