๐ค Extra or Missing Fields in JSON? The Untold Spring Boot Story!
Have you ever sent a JSON request from Postman thinking, “Ah, backend will understand me!”… only to later find out half your fields vanished into thin air? ๐
๐ฏ Scenario 1 — Missing Fields in Request
public class Employee {
private String name;
private String department;
private int age;
private String email;
}
Postman JSON:
{
"name": "Alice",
"department": "HR"
}
Result:
Field | Value |
---|---|
name | Alice |
department | HR |
age | 0 (default int) |
null |
๐ Why? Jackson only maps what it sees in JSON. The rest remain null
(or default for primitives).
๐ฏ Scenario 2 — Extra Fields in Request
public class Employee {
private String name;
private String department;
}
Postman JSON:
{
"name": "Bob",
"department": "Finance",
"age": 30,
"location": "London"
}
Result: Only name
and department
are populated. age
and location
vanish like that friend who said “I’ll be there in 5 minutes” ⏳๐
๐ Who’s Doing the Magic?
- Spring MVC receives the request ๐ฌ
MappingJackson2HttpMessageConverter
turns JSON → Java- Jackson’s
ObjectMapper
maps matching fields - Unknown fields are ignored (by default)
๐ซ When Will It Throw an Error?
- Global Config:
spring.jackson.deserialization.fail-on-unknown-properties=true
- Per Class:
@JsonIgnoreProperties(ignoreUnknown = false)
- Data type mismatch (e.g., sending "thirty" for an int)
- Using constructor/record without matching parameters
- Bean Validation failures (
@NotNull
,@Valid
)
✅ Validating Missing Fields
import jakarta.validation.constraints.NotNull;
public class Employee {
@NotNull(message = "Name is required")
private String name;
@NotNull(message = "Department is required")
private String department;
}
Controller:
import org.springframework.validation.annotation.Validated;
@RestController
@Validated // Needed for method-level validation
public class EmployeeController {
@PostMapping("/create")
public ResponseEntity create(@Valid @RequestBody Employee employee) {
// validation happens before this line
return ResponseEntity.ok("Employee created");
}
}
๐ก Important:
- For request body DTO validation,
@Valid
alone is enough on the method parameter. - For method-level validation inside the same bean, you must also add
@Validated
on the class.
๐ Best Practices
- Use DTOs instead of directly binding to entities
- Validate with
@Valid
and Bean Validation - Enable strict mode in dev/test, allow leniency in prod during deployments
- Document your API to avoid “ghost fields” ๐ป
๐ค Interview Q&A Corner
Q: What happens if your JSON request has more fields than your DTO?
A: By default, extra fields are ignored by Jackson.
Q: How can you make Spring Boot fail on unknown fields?
A: Use spring.jackson.deserialization.fail-on-unknown-properties=true
or @JsonIgnoreProperties(ignoreUnknown = false)
.
Q: How do you enforce certain fields to always be present?
A: Add @NotNull
with @Valid
and, if validating inside the same bean, also @Validated
at the class level.
Q: Which Spring component handles JSON → Java object mapping?
A: MappingJackson2HttpMessageConverter
with Jackson’s ObjectMapper
.
๐ Wrapping Up
Today we busted a common myth — extra fields in JSON won’t break your Spring Boot app unless you tell them to. Missing fields? They just become null or default values. But remember — just because your app doesn’t break, doesn’t mean your data is correct! ๐ง
๐ฌ Have you ever chased a bug for hours, only to find out the frontend was sending “bonus” fields the backend never saved? Share your story in the comments — let's laugh (and cry) together! ๐
Comments
Post a Comment