Backend Development 8 min read

Using @JsonView in Spring to Control JSON Serialization of Fields

This article explains how the Jackson @JsonView annotation can be used in Spring back‑end projects to selectively serialize object fields, reduce bandwidth, improve security, and handle nested associations by defining view interfaces and applying them on entity fields and controller methods.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Using @JsonView in Spring to Control JSON Serialization of Fields

Introduction

In front‑end/back‑end separated projects, JSON strings are used for communication. By default the back‑end returns all fields of an object, which may waste bandwidth or expose sensitive data such as passwords. The Jackson @JsonView annotation provides a way to control which fields are returned from the C‑layer.

Effect of JsonView

Four scenarios are demonstrated with a small demo containing three entities – Teacher, Student, and Klass – and their relationships (many‑to‑one).

1. Without JsonView

All fields, including associated objects, are returned:

{
  "id": 1,
  "name": "学生1",
  "sno": "123456",
  "klass": {
    "id": 1,
    "teacher": {
      "id": 1,
      "name": "张三",
      "sex": false,
      "username": "zhangsan",
      "email": "[email protected]"
    },
    "name": "班级1"
  }
}

Conclusion 1: All fields, including foreign‑key objects, are returned.

2. JsonView on entity fields

Only the fields marked with a specific view are serialized. Example returns only the student name:

{
  "name": "学生1"
}

Conclusion 2: Only fields annotated with the chosen JsonView interface are returned.

3. JsonView on associated objects

When the view is applied to the association itself, the result contains an empty object:

{
  "klass": { }
}

Conclusion 3: Using JsonView on a foreign‑key object returns the object itself but no inner fields.

4. JsonView on fields of associated objects

By defining a view that inherits the required fields of the associated entity, the nested fields are included:

{
  "klass": {
    "teacher": { },
    "name": "班级1"
  }
}

Conclusion 4: To return fields of a related object, the view must inherit the interfaces of those fields.

How to Use JsonView in a Spring Project

1. Define view interfaces in the entity class

Example:

// Define an interface for a specific view
public interface SnoJsonView {}

2. Annotate entity fields with @JsonView

Apply the view to the field you want to control:

@JsonView(SnoJsonView.class)
private String sno;

3. Create composite view interfaces

When multiple fields need to be returned, create an interface that extends all required view interfaces. Example:

public interface GetByIdJsonView extends Student.KlassJsonView, Student.NameJsonView, Student.SnoJsonView {}

Another example that also includes Klass fields:

public interface GetByIdJsonView extends Student.KlassJsonView, Klass.NameJsonView, Klass.TeacherJsonView {}

4. Apply @JsonView on controller methods

Finally, annotate the controller method with the composite view:

@GetMapping("{id}")
@JsonView(GetByIdJsonView.class)
public Student getById(@PathVariable Long id) {
    return this.studentService.findById(id);
}

With this setup, the response JSON contains only the fields declared in the selected view, improving bandwidth usage, enhancing security, and keeping the codebase clean and maintainable.

Summary

In a front‑end/back‑end separated architecture, using JsonView allows developers to precisely control which fields are serialized and sent to the client. Without JsonView, all fields—including related objects—are returned. With JsonView, only the fields declared in the view interface are included, and nested objects can be selectively exposed by extending the appropriate view interfaces.

BackendJavaSerializationSpringJacksonRESTJsonView
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

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.