If you're working with Java and JSON, the Jackson library is likely part of your tech stack. One of the most powerful yet underused annotations it provides is @JsonFormat.
In this ultimate guide, we’ll break down what @JsonFormat does, when to use it, and how to handle advanced scenarios with fully working examples. This post is optimized for SEO to help developers find quick and accurate answers.
✅ What is @JsonFormat in Jackson?
@JsonFormat is a Jackson annotation that helps control how Java fields are serialized (Java to JSON) or deserialized (JSON to Java). This is especially useful for dates, enums, and number formats.
Import Statement:
import com.fasterxml.jackson.annotation.JsonFormat;
Dependencies
Make sure you include the correct Jackson modules for java.time:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.15.3</version>
</dependency>
And register it:
ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JavaTimeModule());