Springboot Json Serialization and Deserialization
📌 JSON Serialization & Deserialization in Spring Boot
Section titled “📌 JSON Serialization & Deserialization in Spring Boot”1. Core Concept
Section titled “1. Core Concept”- Serialization → Java Object ➝ JSON String (Response to Client).
- Deserialization → JSON String ➝ Java Object (Request from Client).
- Spring Boot uses Jackson library by default (via
MappingJackson2HttpMessageConverter).
2. How It Happens in Spring Boot
Section titled “2. How It Happens in Spring Boot”🔹 Serialization (Response Flow)
Section titled “🔹 Serialization (Response Flow)”- Controller returns a Java object.
- Spring MVC finds a suitable HttpMessageConverter.
- For
application/json, it uses MappingJackson2HttpMessageConverter. - This internally uses Jackson’s ObjectMapper to convert object ➝ JSON.
- JSON is written to the HTTP Response Body.
🔹 Deserialization (Request Flow)
Section titled “🔹 Deserialization (Request Flow)”- Client sends JSON request body.
- Spring MVC detects
@RequestBodyon method parameter. - MappingJackson2HttpMessageConverter is selected for JSON.
- Jackson ObjectMapper maps JSON ➝ Java object.
- The controller method gets the populated Java object.
3. Important Classes Involved
Section titled “3. Important Classes Involved”- ObjectMapper → Core Jackson class for serialization/deserialization.
- MappingJackson2HttpMessageConverter → Bridge between Spring MVC and Jackson.
- HttpMessageConverter → Strategy interface to convert objects to/from HTTP body.
4. Key Annotations (Jackson)
Section titled “4. Key Annotations (Jackson)”@JsonProperty("fieldName")→ Rename JSON property.@JsonIgnore→ Skip field from JSON.@JsonInclude(Include.NON_NULL)→ Exclude null fields.@JsonFormat(pattern="yyyy-MM-dd")→ Format dates.@JsonCreator/@JsonValue→ Custom object creation.
5. Customization Options
Section titled “5. Customization Options”-
Configure ObjectMapper Globally
@Beanpublic ObjectMapper objectMapper() {return new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL).disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);} -
Custom Serializer/Deserializer
public class UserSerializer extends JsonSerializer<User> {@Overridepublic void serialize(User user, JsonGenerator gen, SerializerProvider serializers) throws IOException {gen.writeStartObject();gen.writeStringField("username", user.getName());gen.writeEndObject();}}Usage:
@JsonSerialize(using = UserSerializer.class)private User user;
6. Interview-Level Insights
Section titled “6. Interview-Level Insights”-
Default Library: Jackson (but you can plug in Gson, JSON-B, etc.).
-
Spring Boot Auto-Configuration: Auto-registers Jackson’s
ObjectMapper. -
Converter Priority: If multiple converters exist, Spring chooses based on
Content-Type&Acceptheaders. -
Common Interview Qs:
- How does Spring Boot convert objects to JSON internally?
→ Through
MappingJackson2HttpMessageConverterusingObjectMapper. - How to ignore null values in JSON response?
→ Use
@JsonInclude(Include.NON_NULL)or configureObjectMapper. - What happens if a JSON field doesn’t match Java field?
→ By default, Jackson ignores unknown fields (
FAIL_ON_UNKNOWN_PROPERTIES = false). Can be configured. - Can you replace Jackson? → Yes, by excluding Jackson dependency & adding Gson or JSON-B.
- How does Spring know which converter to use?
→ By checking the
Content-Typeof the request &Acceptheader of the response.
- How does Spring Boot convert objects to JSON internally?
→ Through
One-Liner Summary for Interviews:
In Spring Boot, JSON serialization (Java ➝ JSON) and deserialization (JSON ➝ Java) are handled automatically by MappingJackson2HttpMessageConverter, which delegates to Jackson’s ObjectMapper. Behavior can be customized via Jackson annotations or by configuring the ObjectMapper bean.