Backend Development 8 min read

Mastering @JsonFormat in Spring Boot 3: Advanced Date & Enum Handling

This article demonstrates how to use Spring Boot 3's @JsonFormat annotation for precise date, time, and enum serialization, covering configuration via properties, annotation attributes, locale settings, shape options, lenient parsing, and case‑insensitive deserialization with practical code examples.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Mastering @JsonFormat in Spring Boot 3: Advanced Date & Enum Handling

1. Introduction

@JsonFormat is a Jackson annotation that allows fine‑grained control over how Java object fields are serialized and deserialized to JSON, commonly used for dates, times, numbers, and enums. It can transform a Date into a custom string such as "2025-12-31" or an ISO‑8601 timestamp with timezone.

2. Practical Cases

2.1 Date Formatting

By default Spring Boot 3.4.2 returns dates in ISO‑8601 extended format (e.g., 2025-04-26T01:47:11.428+00:00 ), which may not match the desired yyyy-MM-dd HH:mm:ss pattern and timezone. Two solutions are provided:

Define the format and timezone in application.yml :

<code>spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8</code>

Use the @JsonFormat annotation directly on the field:

<code>@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime = new Date();</code>

2.2 Controlling Format and Automatic Error Repair

Output dates as an array:

<code>@JsonFormat(shape = Shape.ARRAY, timezone = "GMT+8")
private LocalDate date = LocalDate.now();</code>

Enable lenient parsing to automatically correct invalid dates:

<code>@JsonFormat(
  pattern = "yyyy-MM-dd HH:mm:ss",
  timezone = "GMT+8",
  lenient = OptBoolean.TRUE)
private Date createTime = new Date();</code>

2.3 Specifying Locale and Output Type

Customize the locale to produce language‑specific month and period strings:

<code>@JsonFormat(pattern = "yyyy MMMM dd a", locale = "en")
private Date createTime = new Date();</code>

For numeric timestamps, set the shape to NUMBER :

<code>@JsonFormat(shape = Shape.NUMBER, timezone = "GMT+8")
private Date createTime = new Date();</code>

2.4 Case‑Insensitive Deserialization

When incoming JSON keys have inconsistent casing, enable case‑insensitive property handling:

<code>@JsonFormat(
  with = JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
public class User {}
</code>

2.5 Enum Serialization

Define an enum with additional fields:

<code>public enum UserStatus {
  ACTIVE("活跃", 100, true),
  INACTIVE("不活跃", 101, false),
  PENDING("待激活", 102, false);
  private final String description;
  private final int code;
  private final boolean isActive;
  UserStatus(String description, int code, boolean isActive) {
    this.description = description;
    this.code = code;
    this.isActive = isActive;
  }
  public String getDescription() { return description; }
  public int getCode() { return code; }
  public boolean isActive() { return isActive; }
}
</code>

Serialize the enum as a JSON object to include all fields:

<code>@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum UserStatus {}
</code>

Alternatively, configure Jackson to use toString() for enums:

<code>spring:
  jackson:
    serialization:
      write-enums-using-to-string: true
</code>
<code>@Override
public String toString() {
  return description + "/" + code + "/" + isActive;
}
</code>

3. Supporting Code Samples

Entity and controller examples used throughout the article:

<code>public class User {
  private Long id;
  private String firstName;
  private String lastName;
  private Date createTime = new Date();
  // getters, setters, constructors
}
</code>
<code>@RestController
@RequestMapping("/users")
public class UserController {
  @GetMapping("/{id}")
  public ResponseEntity<User> query(@PathVariable Long id) {
    return ResponseEntity.ok(new User(id, "pack", "xg", new Date()));
  }
}
</code>

4. Visual Results

Default ISO‑8601 output:

Formatted date using configuration:

Locale‑specific output (English):

Locale‑specific output (Chinese):

Enum serialized as object:

Enum serialized using toString :

JavaSpring BootJacksondate formatting@JsonFormat
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.