How can you handle date formatting in Jackson?
A) By using the @JsonFormat annotation
B) By using the @JsonProperty annotation
C) By using the @JsonIgnore annotation
D) By setting a flag in the JSON
Answer:
A) By using the @JsonFormat annotation
Explanation:
In Jackson, you can handle date formatting by using the @JsonFormat
annotation. This annotation allows you to specify the format in which date and time fields should be serialized and deserialized.
For example:
public class SomeClass {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private Date date;
// other fields
}
In this example, the date
field will be serialized and deserialized in the “yyyy-MM-dd” format, ensuring consistent and readable date formats in the JSON output.