Understanding and Overcoming the Boolean Value Issue in Spring Boot DTOs
The Problem: Boolean Value Is null in DTOs
When developing REST APIs using Spring Boot, developers may encounter a frustrating issue: boolean values sent in request JSON payloads become null in the corresponding DTOs. This problem is not a bug in Spring Boot but rather a consequence of Jackson and Java Bean naming conventions.
A Real-World Example
Consider a request payload:
{ "isActive": true } The corresponding DTO:
public class UserRequest { private Boolean isActive; // getters and setters } And the controller code:
@PostMapping("/user") public void createUser(@RequestBody UserRequest request) { System.out.println(request.getIsActive()); } The output: null
Why This Happens (Jackson + Java Bean Rules)
Spring Boot uses Jackson to convert JSON into Java objects. Jackson does not rely only on field names; it follows Java Bean naming conventions, which depend heavily on getter and setter names. When it comes to boolean fields, Jackson expects certain getter and setter method patterns.
Jackson's Expectations
For the field: private Boolean isActive;
Jackson expects one of these method patterns:
public Boolean isActive() or public Boolean getActive()
But what we actually wrote: public Boolean getIsActive()
Solution 1: Use @JsonProperty (Quick Fix)
Explicitly telling Jackson how to map the JSON property fixes the issue:
import com.fasterxml.jackson.annotation.JsonProperty; public class UserRequest { @JsonProperty("isActive") private Boolean isActive; // getters and setters } Solution 2: Best Practice (Highly Recommended)
Instead of fixing the mapping, fix the design. Avoid starting boolean field names with is.
public class UserRequest { private Boolean active; // getters and setters } Why This Is Better
This design is fully Java Bean compliant, requires no Jackson annotations, results in cleaner, more readable DTOs, and minimizes bugs in large codebases.
boolean vs Boolean in Spring Boot DTOs
Type: Default Value, Nullable
boolean: falseBoolean: null
When to Use What: Use boolean when the field is mandatory. Use Boolean when the field is optional or tri-state.
Key Takeaways
- Boolean fields starting with
iscan silently break Jackson mapping - Jackson relies on getter/setter naming, not field names
- @JsonProperty fixes the issue explicitly
- Best practice: avoid
isprefix in DTO fields - Always check DTO design before debugging controllers
Final Advice
If your JSON looks correct but your DTO value is null, check the getter and setter naming first. This small detail can save hours of debugging in Spring Boot applications.