Unlock Advanced JSON Control in Spring Boot 3 with Rare Jackson Annotations

This article demonstrates how to use four lesser‑known Jackson annotations—@JsonAppend, @JsonNaming, @JsonPropertyDescription, and @JsonPOJOBuilder—to extend JSON serialization, customize property naming, enrich JSON Schema documentation, and deserialize immutable objects within Spring Boot 3 applications.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Unlock Advanced JSON Control in Spring Boot 3 with Rare Jackson Annotations

Environment

Spring Boot 3.4.2

1. Introduction

Jackson is the default JSON library in Spring Boot, offering efficient, flexible serialization and deserialization of complex objects, supporting Java Bean, collections, generics, and Java 8+ features such as Optional and LocalDateTime. Annotations like @JsonIgnore and @JsonProperty allow fine‑grained field control, and MixIn enables non‑intrusive extensions.

This article introduces four powerful but relatively obscure Jackson annotations that can significantly improve flexibility and efficiency in specific scenarios.

2. Practical Cases

2.1 @JsonAppend

@JsonAppend adds virtual properties to an object during serialization without modifying the class definition, useful for injecting additional information such as version data.

Example entity:

public class App {
  private String title;
  private String author;
  @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
  private Date createAt;
}

Desired output includes an extra version field.

Using @JsonAppend:

@JsonAppend(attrs = {
    @JsonAppend.Attr(value = "version")
})
public class App {
  private String title;
  private String author;
  @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
  private Date createAt;
}

Controller snippet to supply the version attribute:

private final ObjectMapper objectMapper;
public AppController(ObjectMapper objectMapper) {
  this.objectMapper = objectMapper;
}
@GetMapping(produces = "application/json")
public ResponseEntity<String> query() throws Exception {
  App app = new App("Spring Boot3实战案例200讲", "Pack_xg", new Date());
  ObjectWriter writer = objectMapper.writerFor(App.class).withAttribute("version", "1.0.0");
  return ResponseEntity.ok(writer.writeValueAsString(app));
}

Resulting JSON contains the additional version field.

2.2 @JsonNaming

@JsonNaming specifies a property naming strategy, overriding the default camelCase. Supported strategies include KEBAB_CASE, LOWER_CASE, SNAKE_CASE, and UPPER_CAMEL_CASE.

Example entity using UpperCamelCaseStrategy:

@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy.class)
public class Author {
  private String firstName;
  private String lastName;
  private Integer age;
  private String idNo;
}

Controller returns an Author instance, and the JSON keys follow UpperCamelCase.

2.3 @JsonPropertyDescription

@JsonPropertyDescription adds human‑readable descriptions to JSON Schema properties, improving documentation and validation clarity.

Dependency required:

<dependency>
  <groupId>com.fasterxml.jackson.module</groupId>
  <artifactId>jackson-module-jsonSchema</artifactId>
</dependency>

Example entity:

public class Category {
  private Long id;
  @JsonPropertyDescription("商品分类名称,该属性是必须字段")
  private String name;
  @JsonPropertyDescription("分类状态,只有值为1时可用")
  private Integer state;
}

Controller generates a JsonSchema for Category.

Combining with @JsonProperty(required = true) marks a field as mandatory in the schema.

2.4 @JsonPOJOBuilder

@JsonPOJOBuilder defines a custom builder class for deserializing immutable objects, used together with @JsonDeserialize(builder = …).

Example immutable Student class with builder:

@JsonDeserialize(builder = Student.Builder.class)
public final class Student {
  private final String name;
  private final int age;
  private Student(Builder builder) {
    this.name = builder.name;
    this.age = builder.age;
  }
  public String getName() { return name; }
  public int getAge() { return age; }

  @JsonPOJOBuilder(buildMethodName = "build", withPrefix = "set")
  public static class Builder {
    private String name;
    private int age;
    public Builder setName(String name) { this.name = name; return this; }
    public Builder setAge(int age) { this.age = age; return this; }
    public Student build() { return new Student(this); }
  }
}

Controller endpoint to save a Student simply returns the received object.

These annotations enable advanced JSON manipulation, schema generation, and immutable object handling within Spring Boot applications.

backendserializationJSONSpring 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.