Q1. How does Spring handle JSON serialization/deserialization?
Spring uses
The
Jackson is included by default in Spring Boot.
HttpMessageConverter implementations.The
MappingJackson2HttpMessageConverter (provided by Jackson) automatically converts Java objects to JSON and vice versa when @ResponseBody or @RequestBody is used.Jackson is included by default in Spring Boot.
Q2. How can you customize JSON serialization with Jackson?
You can use Jackson annotations like
For global configuration, you can customize
In Spring Boot, properties like
@JsonIgnore, @JsonProperty, @JsonFormat, @JsonInclude.For global configuration, you can customize
ObjectMapper bean.In Spring Boot, properties like
spring.jackson.* can be set.Q3. What is @JsonIgnore used for?
Useful for hiding sensitive data like passwords or avoiding circular references.
@JsonIgnore is used to mark a property to be excluded from JSON serialization and deserialization.Useful for hiding sensitive data like passwords or avoiding circular references.
Q4. How do you handle date formatting in JSON?
Use
@JsonFormat annotation:@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private Date createdDate;
You can also configure default date format in application properties.Q5. What is the difference between @JsonIgnore and @JsonIgnoreProperties?
It can also be used to ignore unknown properties during deserialization.
@JsonIgnore is used on fields, getters, or setters to ignore a single property.@JsonIgnoreProperties is used at class level to ignore multiple properties, e.g., @JsonIgnoreProperties({"id", "version"}).It can also be used to ignore unknown properties during deserialization.
