Backend Development 13 min read

Master Jackson Annotations in Spring Boot 3.2.5: From @JsonAnyGetter to Custom Annotations

This tutorial explores Jackson's rich annotation set in Spring Boot 3.2.5, demonstrating how to serialize and deserialize JSON with built‑in annotations like @JsonAnyGetter, @JsonGetter, @JsonPropertyOrder, and @JsonIgnore, how to create custom annotations, and how to disable annotation processing for fine‑grained control.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master Jackson Annotations in Spring Boot 3.2.5: From @JsonAnyGetter to Custom Annotations

Environment: Spring Boot 3.2.5

1. Introduction

Spring Boot integrates three JSON mapping libraries—Gson, Jackson, and JSON‑B—with Jackson being the default choice. This article dives deep into Jackson annotations, showing how to use existing ones, create custom annotations, and disable them when needed.

2. Practical examples

2.1 Serialization annotations

@JsonAnyGetter – Automatically maps the key/value pairs of a Map to ordinary JSON properties. <code>public class User { private String name; private Map<String, String> properties; // getters, setters }</code> <code>@GetMapping("/jsonanygetter") public User jsonanygetter() { User user = new User("张三", Map.of("key1", "value1", "key2", "value2")); return user; }</code> Default output shows the map entries as separate fields. Adding @JsonAnyGetter to the map getter expands the map entries into normal properties. <code>@JsonAnyGetter public Map<String, String> getProperties() { return properties; }</code> Disabling the annotation with @JsonAnyGetter(enabled = false) restores the default behavior.

@JsonGetter – Marks a method as a getter for a JSON property, similar to @JsonProperty . <code>public class User { private String name; private Map<String, String> properties; // getters, setters @JsonGetter public String info() { return "info - " + new Random().nextInt(10000); } }</code> Output maps the method name info to a JSON field. The name can be customized: <code>@JsonGetter("fullinfo") public String info() { ... }</code>

@JsonPropertyOrder – Specifies the order of JSON fields. <code>@JsonPropertyOrder({"fullinfo", "name", "properties"}) public class User { } </code>

@JsonRawValue – Serializes a property exactly as raw JSON. <code>public class User { private String name; private Map<String, String> properties; @JsonRawValue private String json; }</code> <code>@GetMapping("/jsonanygetter") public User jsonanygetter() { User user = new User("张三", Map.of("key1", "value1", "key2", "value2")); user.setJson(""" { "age": 22, "birthday": "1999-12-12" } """); return user; }</code>

@JsonValue – Uses a single method to serialize the whole instance, often applied to enums. <code>public enum Gender { FEMALE(1, "女"), MALE(2, "男"); private Integer code; private String name; private Gender(Integer code, String name) { this.code = code; this.name = name; } @JsonValue public String getName() { return name; } }</code> When placed on a property of another object, only that property's value is emitted. <code>public class Address { private Long id; private String city; private String details; @JsonValue public String getDetails() { return details; } }</code>

@JsonIgnoreProperties – Ignores specified fields during serialization. <code>@JsonIgnoreProperties({"id"}) public record Customer(Long id, String name) { }</code>

@JsonIgnore – Skips a single field. <code>public record Customer(@JsonIgnore Long id, String name) { }</code>

@JsonInclude – Excludes null/empty/default values. <code>@JsonInclude(Include.NON_NULL) public class MyBean { public Integer id; public String name; }</code> <code>@GetMapping("/jsoninclude") public MyBean jsoninclude() { MyBean bean = new MyBean(); bean.name = "中国🇨🇳"; return bean; }</code>

@JsonIncludeProperties – Includes only specified properties (Jackson 2.12+). <code>@JsonIncludeProperties({"name", "email"}) public class People { private String name; private String sex; private Integer age; private String email; }</code>

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

2.2 Deserialization annotations

@JsonCreator – Specifies a constructor or factory method for deserialization. <code>public static class Order { private String orderNo; private BigDecimal amount; @JsonCreator public Order(@JsonProperty("no") String orderNo, BigDecimal amount) { this.orderNo = orderNo; this.amount = amount; } }</code>

@JsonAnySetter – Collects unknown properties into a map. <code>public static class MockBean { private String name; private Map<String, String> properties = new HashMap<>(); @JsonAnySetter public void add(String key, String value) { properties.put(key, value); } // getters, setters }</code>

@JsonSetter – Marks a method as a setter for a JSON property, optionally renaming it. <code>public class MyObject { private Long id; @JsonSetter("fistName") private String name; private String infos; // getters, setters }</code>

@JsonAlias – Provides alternative names for a property during deserialization. <code>public static class AliasObject { @JsonAlias({"fName", "f_name", "fullName"}) private String name; }</code>

2.3 Other useful annotations

@JsonIgnoreType – Ignores all properties of a given type. <code>public record Customer(@JsonIgnore Long id, String name, Other other) { } @JsonIgnoreType public record Other(String info, String details) { }</code>

2.4 Custom annotations

Combine multiple Jackson annotations into a single meta‑annotation using @JacksonAnnotationsInside :

<code>@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonInclude(Include.NON_NULL)
@JsonPropertyOrder({"name", "id", "email"})
public @interface Packnnotation { }

@Packnnotation
public class Pack {
    public Integer id;
    public String name;
    public String email;
}</code>

2.5 Disabling annotations

Jackson can be configured to ignore all annotations:

<code>@JsonInclude(Include.NON_NULL)
@JsonPropertyOrder({"name", "id"})
public class PackObject {
    public Integer id;
    public String name;
    public String email;
}

@GetMapping("/disableannotation")
public String disableannotation() throws Exception {
    JsonMapper mapper = JsonMapper.builder().disable(MapperFeature.USE_ANNOTATIONS).build();
    String result = mapper.writeValueAsString(new PackObject(1, "张三", null));
    return result;
}</code>
SerializationJSONSpring BootCustom AnnotationAnnotationsJacksondeserialization
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.