Spring Boot
JSON Serialization
Ignore Null Values
Spring Boot 1.2.3
Java

For Spring Boot 1.2.3, how to set ignore null value in JSON serialization?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Spring Boot 1.2.3 uses Jackson for JSON serialization, so ignoring null values is really a Jackson configuration problem. In that version, you can solve it either globally through Spring Boot configuration or programmatically through the object mapper setup, and you can also control it at the individual class level with @JsonInclude.

Use Jackson's Non-Null Inclusion Rule

At the class level, the basic Jackson answer is JsonInclude.Include.NON_NULL.

java
1import com.fasterxml.jackson.annotation.JsonInclude;
2
3@JsonInclude(JsonInclude.Include.NON_NULL)
4public class UserResponse {
5    private String username;
6    private String displayName;
7    private String phoneNumber;
8
9    public String getUsername() {
10        return username;
11    }
12
13    public void setUsername(String username) {
14        this.username = username;
15    }
16
17    public String getDisplayName() {
18        return displayName;
19    }
20
21    public void setDisplayName(String displayName) {
22        this.displayName = displayName;
23    }
24
25    public String getPhoneNumber() {
26        return phoneNumber;
27    }
28
29    public void setPhoneNumber(String phoneNumber) {
30        this.phoneNumber = phoneNumber;
31    }
32}

If displayName or phoneNumber is null, Jackson leaves it out of the JSON output.

This is the most direct option when only certain DTOs should omit null values.

Configure It Globally In Spring Boot 1.2.3

Spring Boot 1.2.3 also exposes Jackson configuration through properties. The usual global setting is:

properties
spring.jackson.serialization-inclusion=non_null

Or in YAML:

yaml
spring:
  jackson:
    serialization-inclusion: non_null

With this property in place, Spring Boot configures the application's Jackson mapper to skip null values globally.

This is the simplest answer if your whole API should omit null fields by default.

Programmatic Configuration Is Also Possible

If you prefer Java configuration, customize the object mapper builder or object mapper itself.

java
1import com.fasterxml.jackson.annotation.JsonInclude;
2import org.springframework.context.annotation.Bean;
3import org.springframework.context.annotation.Configuration;
4import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
5
6@Configuration
7public class JacksonConfig {
8
9    @Bean
10    public Jackson2ObjectMapperBuilder jacksonBuilder() {
11        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
12        builder.serializationInclusion(JsonInclude.Include.NON_NULL);
13        return builder;
14    }
15}

This gives you explicit Java-side control, which is useful if configuration depends on code or if you want the setting documented in one central configuration class.

Understand The Scope Of The Choice

These approaches differ by scope:

  • '@JsonInclude affects one class or one field'
  • the Spring Boot property affects the application's default mapper
  • Java configuration affects the mapper globally unless you create multiple mappers intentionally

That distinction matters because ignoring nulls is an API design choice, not just a formatting tweak.

For example, some clients rely on explicit null to distinguish:

  • field intentionally unknown
  • field absent because not applicable

If you globally remove nulls, those two cases may collapse into one.

A Simple Controller Example

java
1import org.springframework.web.bind.annotation.GetMapping;
2import org.springframework.web.bind.annotation.RestController;
3
4@RestController
5public class UserController {
6
7    @GetMapping("/user")
8    public UserResponse getUser() {
9        UserResponse response = new UserResponse();
10        response.setUsername("mark");
11        response.setDisplayName(null);
12        response.setPhoneNumber(null);
13        return response;
14    }
15}

With non-null inclusion enabled, the response becomes something like:

json
{
  "username": "mark"
}

Without that configuration, Jackson would also emit the null-valued fields.

Field-Level Control Still Works

If only one property should be excluded when null, you can annotate the field directly.

java
1import com.fasterxml.jackson.annotation.JsonInclude;
2
3public class AccountView {
4    private String id;
5
6    @JsonInclude(JsonInclude.Include.NON_NULL)
7    private String nickname;
8
9    public String getId() {
10        return id;
11    }
12
13    public void setId(String id) {
14        this.id = id;
15    }
16
17    public String getNickname() {
18        return nickname;
19    }
20
21    public void setNickname(String nickname) {
22        this.nickname = nickname;
23    }
24}

That is useful when you want global defaults unchanged but need one field handled specially.

Common Pitfalls

The biggest mistake is applying a global non-null rule without checking whether API consumers depend on explicit null fields.

Another mistake is mixing class-level and global rules without documenting the intended behavior. The result can become hard to reason about across different DTOs.

People also forget that the setting affects serialization output, not the Java object itself. The fields are still null in memory; they are simply omitted from JSON.

Finally, when maintaining an older Spring Boot version, make sure you use the configuration keys supported by that version instead of copying examples from newer Boot releases blindly.

Summary

  • In Spring Boot 1.2.3, ignoring null JSON values is handled through Jackson.
  • Use @JsonInclude(Include.NON_NULL) for class-level or field-level control.
  • Use spring.jackson.serialization-inclusion=non_null for a global application-wide default.
  • Java configuration with Jackson2ObjectMapperBuilder is a good explicit alternative.
  • Treat null omission as an API contract decision, not just a cosmetic serialization setting.

Course illustration
Course illustration