Master Jackson in Spring Boot 3: Real‑World JSON Tricks & Examples

This tutorial walks through Jackson usage in Spring Boot 3, covering annotations for ignoring fields, renaming properties, custom serialization/deserialization, constructor handling, property visibility, date formatting, efficient JSON read/write, and enum handling with clear code samples and output screenshots.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master Jackson in Spring Boot 3: Real‑World JSON Tricks & Examples

1. Introduction

Jackson is the default JSON serializer/deserializer in Spring Boot projects. It offers efficient and flexible data exchange, and its rich annotations let developers control the format and content of returned data.

2. Practical Cases

2.1 Ignoring Properties

Use @JsonIgnore on fields or @JsonIgnoreProperties on a class to prevent sensitive data from being serialized.

public class User {
    private Long id;
    private String name;
    // This field will not be returned to the client
    @JsonIgnore
    private String idNo;
    private Date birthday;
    private String user_name;
    // getters, setters
}

@GetMapping("/ignore")
public User ignore() {
    return new User(1L, "张三", "11111111", new Date());
}

Multiple fields can be ignored at once with @JsonIgnoreProperties({"idNo", "birthday"}) on the class.

@JsonIgnoreProperties({"idNo", "birthday"})
public class User {}

2.2 Redefining Field Names

Use @JsonProperty("userName") to rename a field in the JSON output without changing the Java field name.

@JsonProperty("userName")
private String user_name;

2.3 Custom Serialization/Deserialization

When a property is declared as a superclass but you want to deserialize/serialize a specific subclass, use @JsonDeserialize(as = Shot.class) or @JsonSerialize(as = Shot.class).

public class Sport { private String name; }
public class Run extends Sport { private Integer speed; }
public class Shot extends Sport { private Integer score; }

public static class User { private Sport sport; }

@PostMapping("/ignore2")
public User ignore2(@RequestBody User user) { return user; }

@JsonDeserialize(as = Shot.class)
private Sport sport;

2.4 Specifying Constructors

Jackson uses the default no‑arg constructor by default. To use a parameterized constructor, annotate it with @JsonCreator and bind parameters with @JsonProperty.

@JsonCreator
public Shot(@JsonProperty("score") Integer score) {
    this.score = score;
}

2.5 Property Visibility

By default Jackson detects public fields, public getters, and any setters. To make all fields visible regardless of visibility, add

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)

.

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class Person { private int age; private String name; public Person() {} public Person(int age, String name) { this.age = age; this.name = name; } public String getName() { return name; } }

After adding the annotation, the output includes both fields.

2.6 Date Formatting

Use

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", shape = JsonFormat.Shape.STRING, timezone = "GMT+8")

on a date field, or configure spring.jackson.date-format and spring.jackson.time-zone in application.yml. For Java 8 time types like Instant, the annotation is required because the global configuration does not apply.

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", shape = Shape.STRING, timezone = "GMT+8")
private Date birthday;

When using Instant, only the annotation works:

private Instant createTime;

2.7 Efficient JSON Read/Write

Create an ObjectMapper to parse JSON strings into a JsonNode and to modify or generate JSON.

ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree("{\"name\":\"张三\",\"age\":22,\"sport\":{\"name\":\"跑步\"}}");
String name = root.get("name").asText();
int age = root.get("age").asInt();
String sportName = root.get("sport").get("name").asText();

root.withObject("/extra").put("message", "this's message");
String json = mapper.writeValueAsString(root);
System.out.println(json);

2.8 Enum Types

Define enums for fixed sets of values and control their JSON representation with @JsonValue. Jackson can both serialize and deserialize enum fields.

public class Student {
    private String sno;
    private GenderEnum gender;
}
public enum GenderEnum {
    MALE(0, "男"),
    FEMALE(1, "女");
    private int code;
    private String label;
    @JsonValue
    public String getLabel() { return label; }
    GenderEnum(int code, String label) { this.code = code; this.label = label; }
}
@GetMapping("/student")
public Student student() {
    Student s = new Student();
    s.setSno("S0001");
    s.setGender(GenderEnum.MALE);
    return s;
}
@PostMapping("/student")
public Student save(@RequestBody Student student) { return student; }
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentJSONSpring BootAnnotationsjackson
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

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.