Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 18

    JSON Handling

    Spring Boot uses Jackson for JSON by default on starter-web.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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

    1. Return OrderView and inspect JSON keys.
    2. Add an unknown field in a POST body and observe default behavior.
    3. Configure fail-on-unknown if you want stricter contracts.
    4. Keep amountCents as int/long — never double.

    Informative example

    Strict unknown properties for write DTOs.

    java
    @Bean
    Jackson2ObjectMapperBuilderCustomizer ordersJackson() {
    return builder -> builder.featuresToEnable(
    DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    }

    Request record the client posts:

    java
    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 admin flags 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/double for money.
    • Renaming JSON fields casually and breaking mobile clients.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionWhat library does Boot use for JSON by default?+

    Answer

    Jackson, via spring-boot-starter-json on the web starter.
    2IntermediateQuestionWhy are records convenient for API payloads?+

    Answer

    Immutable components map cleanly to JSON keys and reduce boilerplate getters while keeping the contract explicit.
    3AdvancedQuestionHow do you avoid mass assignment?+

    Answer

    Use dedicated request DTOs with only client-editable fields; never expose entities as request bodies.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.