- Jackson Annotations Tutorial
- Jackson - Home
- Serialization Annotations
- Jackson - @JsonAnyGetter
- Jackson - @JsonGetter
- @JsonPropertyOrder
- Jackson - @JsonRawValue
- Jackson - @JsonValue
- Jackson - @JsonRootName
- Jackson - @JsonSerialize
- Deserialization Annotations
- Jackson - @JsonCreator
- Jackson - @JacksonInject
- Jackson - @JsonAnySetter
- Jackson - @JsonSetter
- Jackson - @JsonDeserialize
- @JsonEnumDefaultValue
- Property Inclusion Annotations
- @JsonIgnoreProperties
- Jackson - @JsonIgnore
- Jackson - @JsonIgnoreType
- Jackson - @JsonInclude
- Jackson - @JsonAutoDetect
- Type Handling Annotations
- Jackson - @JsonTypeInfo
- Jackson - @JsonSubTypes
- Jackson - @JsonTypeName
- General Annotations
- Jackson - @JsonProperty
- Jackson - @JsonFormat
- Jackson - @JsonUnwrapped
- Jackson - @JsonView
- @JsonManagedReference
- @JsonBackReference
- Jackson - @JsonIdentityInfo
- Jackson - @JsonFilter
- Miscellaneous
- Custom Annotation
- MixIn Annotations
- Disable Annotation
- Jackson Annotations Resources
- Jackson - Quick Guide
- Jackson - Useful Resources
- Jackson - Discussion
Jackson Annotations - @JsonFormat
@JsonFormat is used to specify format while serialization or de-serialization. It is mostly used with Date fields.
Example - @JsonFormat
import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonTester { public static void main(String args[]) throws IOException, ParseException { ObjectMapper mapper = new ObjectMapper(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy"); Date date = simpleDateFormat.parse("20-12-1984"); Student student = new Student(1, date); String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(student); System.out.println(jsonString); } } class Student { public int id; @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy") public Date birthDate; Student(int id, Date birthDate){ this.id = id; this.birthDate = birthDate; } }
Output
{ "id" : 1, "birthDate" : "19-12-1984" }
Advertisements