SoFunction
Updated on 2025-05-11

Detailed explanation of the use of Java Jackson core annotations

Preface

In Java backend development, Jackson is one of the most commonly used JSON serialization/deserialization tools. Jackson provides a series of powerful annotations for precise control of mapping logic between JSON and Java objects. This article will explain in depth several commonly used core annotations:

  • @JsonProperty: Specify the JSON name of the field
  • @JsonIgnore: Ignore field serialization/deserialization
  • @JsonInclude: Controls whether the field participates in serialization (such as null, empty set, etc.)
  • @JsonFormat: Format date and time
  • @JsonCreator / @JsonValue: Serialization/deserialization logic for custom objects

1. @JsonProperty-Specify the JSON field name

By default, Jackson uses Java field names as the JSON field names. However, when the front and back end fields are inconsistent, you can use@JsonPropertyExplicitly specify:

public class User {
    @JsonProperty("user_name")
    private String username;
    // getter/setter
}

Output JSON:

{
  "user_name": "Tom"
}

2. @JsonIgnore-Ignore fields

@JsonIgnoreUsed to skip certain fields during serialization or deserialization, such as passwords, internal fields, etc.:

public class Account {
    private String username;
    @JsonIgnore
    private String password;
    // getter/setter
}

Output JSON does not includepasswordField.

3. @JsonInclude-Contact whether to output null or empty fields

pass@JsonIncludeYou can control whether the field is output. Common options:

  • Include.NON_NULL: Ignore the null value
  • Include.NON_EMPTY: Ignore null, empty strings, empty sets, etc.
@JsonInclude(.NON_NULL)
public class Product {
    private String name;
    private String description;
}

whendescriptionIt will not appear in JSON when null.

It can also be used on classes, fields, or global configuration:

@JsonInclude(.NON_EMPTY)
private List<String> tags;

4. @JsonFormat-Format time

Date types in Java are serialized to timestamp by default. pass@JsonFormatYou can specify formats and time zones:

public class Event {
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date eventTime;
}

The date format in output JSON is:"2025-05-06 16:00:00"

5. @JsonCreator/@JsonValue-custom serialization and deserialization

1. @JsonCreator: Construct method deserialization

Use when the object does not have a default constructor, or when you want to control the deserialization logic:

public class Currency {
    private String code;
    @JsonCreator
    public Currency(@JsonProperty("code") String code) {
         = code;
    }
    public String getCode() { return code; }
}

2. @JsonValue: Custom serialized value

If you want the entire object to output only a certain field when serializing, you can use @JsonValue:

public class Status {
    private final String value;
    public Status(String value) {
         = value;
    }
    @JsonValue
    public String getValue() {
        return value;
    }
}

After serialization, only strings will be output:

"ACTIVE"

6. Complete example

public class User {
    @JsonProperty("user_name")
    private String username;
    @JsonIgnore
    private String password;
    @JsonInclude(.NON_NULL)
    private String email;
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date birthday;
    // getters and setters
}

7. Practical project structure and code warehouse

Sample interface returns serialization results

@RestController
public class UserController {
    @GetMapping("/user")
    public User getUser() {
        User user = new User();
        ("alice");
        ("123456");
        (null);
        (new Date());
        return user;
    }
}

8. Summary

Jackson provides annotations that allow us to accurately control the mapping behavior of objects and JSON. Reasonable use@JsonProperty@JsonIgnore@JsonIncludeSuch annotations can not only meet the needs of front- and back-end fields, but also improve the readability and maintainability of the code.

These annotations are widely used in actual development and are an important tool that every Java developer should master. If you want to build a more powerful JSON data model control system, Jackson annotation system is your right-hand assistant.

The above is the detailed explanation of the use of Java Jackson core annotations. For more information about Java Jackson core annotations, please follow my other related articles!