Master Spring Boot 3 JSON Handling: 12 Essential Jackson Annotations with Real‑World Examples
This article walks through the most common Jackson annotations for Spring Boot 3, explaining their purpose, showing concise code samples, and demonstrating the resulting JSON output with screenshots, so developers can confidently handle naming, ignoring, formatting, dynamic properties, and custom serialization in their APIs.
1. Introduction
In everyday development we constantly interact with JSON—whether building RESTful APIs, calling third‑party services, or communicating between microservices. Mapping JSON fields to Java properties often requires annotations such as @JsonProperty , @JsonIgnore , @JsonFormat , and others to handle naming mismatches, hide sensitive data, enforce date formats, and more.
2. Practical Cases
2.1 @JsonProperty
The @JsonProperty annotation specifies the name of a field during serialization or deserialization, useful when JSON and Java field names differ.
public class Product {
private Long id;
@JsonProperty("productName")
private String name;
private BigDecimal price;
private Integer quantity;
}Controller Interface
@GetMapping("/{id}")
public Product queryProduct(@PathVariable Long id) {
return new Product(id, "Spring Boot3实战案例200讲", new BigDecimal("70"), 60000);
}2.2 @JsonIgnore
@JsonIgnore prevents a field from being serialized or deserialized, ideal for hiding sensitive information.
public class Product {
@JsonIgnore
private Integer quantity;
}2.3 @JsonFormat
@JsonFormat defines the date‑time pattern used during (de)serialization, ensuring consistent formatting across the application.
public class Product {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm", shape = Shape.STRING, timezone = "GMT+8")
private Date createTime;
}2.4 @JsonInclude
@JsonInclude controls which fields are included in the JSON output, e.g., only non‑null values.
public class Product {
@JsonInclude(value = Include.NON_NULL)
private String createUser;
}2.5 @JsonCreator
@JsonCreator customizes deserialization for immutable objects, usually paired with @JsonProperty on constructor parameters.
public class Product {
private final Long id;
@JsonProperty("productName")
private final String name;
@JsonCreator
public Product(@JsonProperty("productId") Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() { return id; }
public String getName() { return name; }
}Controller Interface
@PostMapping
public Product createProduct(@RequestBody Product product) {
return product;
}2.6 @JsonAlias
@JsonAlias defines alternative names for a field during deserialization, allowing multiple possible JSON keys.
public class Product {
private Long id;
@JsonAlias({"productName", "product_name"})
private String name;
}2.7 @JsonIgnoreProperties
@JsonIgnoreProperties applied at class level skips a list of fields during (de)serialization.
@JsonIgnoreProperties({"createTime", "createUser"})
public class Product {
// ...
}2.8 @JsonAnyGetter & @JsonAnySetter
These annotations handle dynamic properties that are not predefined in the class.
public class DynamicProperties {
private Map<String, Object> properties = new HashMap<>();
@JsonAnyGetter
public Map<String, Object> getProperties() { return properties; }
@JsonAnySetter
public void setProperty(String key, Object value) { properties.put(key, value); }
}Controller Interface
@GetMapping("/dy")
public DynamicProperties dy() {
DynamicProperties props = new DynamicProperties();
props.setProperty("name", "Spring Boot3实战案例200讲");
props.setProperty("price", new BigDecimal("70"));
return props;
}2.9 @JsonAutoDetect
@JsonAutoDetect changes visibility rules for fields, getters, setters, and constructors.
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
public class Teacher {
private Long id;
private String name;
public Teacher(Long id, String name) { this.id = id; this.name = name; }
}Controller Interface
@GetMapping("")
public Teacher queryTeacher() {
return new Teacher(666L, "pack_xg");
}2.10 @JsonUnwrapped
@JsonUnwrapped flattens a nested object so its fields appear at the same level as the parent.
public class Customer {
private Long id;
private String name;
@JsonUnwrapped
private Address address;
}
public class Address {
private String province;
private String city;
private String county;
private String street;
}Controller Interface
@GetMapping("")
public Customer queryCustomer() {
Customer customer = new Customer();
customer.setId(666L);
customer.setName("pack_xg");
Address address = new Address();
address.setProvince("新疆");
address.setCity("乌鲁木齐");
address.setCounty("天山区");
address.setStreet("幸福路街道办事处");
customer.setAddress(address);
return customer;
}2.11 @JsonSerializer (Custom Serializer)
Custom serializers give fine‑grained control over how a Java type is converted to JSON.
public class ProductPriceSerializer extends JsonSerializer<BigDecimal> {
@Override
public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString(value.setScale(2, RoundingMode.HALF_UP).toString() + " USD");
}
}Usage:
public class Product {
@JsonSerialize(using = ProductPriceSerializer.class)
private BigDecimal price;
}2.12 @JsonView
@JsonView lets you define multiple JSON views for the same model, exposing different fields to different audiences.
For detailed usage, see the linked article.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
