You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Spring Boot中@JsonInclude(Include.NON_NULL)的作用及使用问询

Great question! Let's break down exactly what @JsonInclude does and why it's such a handy tool for DTOs, using your example to make things crystal clear.

What is @JsonInclude and Why Does It Matter?

This annotation comes from Jackson (the JSON library Spring Boot uses under the hood), and its core job is to control which fields get included when an object is serialized to JSON (or excluded, depending on the rule you set). For DTOs—objects designed to transfer clean, focused data between layers—this is incredibly useful for keeping your JSON payloads lean, uncluttered, and aligned with your business needs.

Let's Walk Through Your Example

Your Code A defines a Greeting DTO with the annotation:

@JsonInclude(JsonInclude.Include.NON_NULL)
class Greeting {
    String name
    String email
}

The NON_NULL value here tells Jackson one simple rule: "Only add this field to the JSON output if it's not null."

What Happens Without the Annotation?

If you left off @JsonInclude (like your incomplete Code B suggests), here's the scenario:

  • A client sends a request body like {"name": "Alejo"} (no email provided).
  • The Greeting object created from this request will have a null value for email.
  • If you were to serialize this object (say, returning it as an API response), the JSON would include the null field: {"name": "Alejo", "email": null}.

What Happens With @JsonInclude(NON_NULL)?

With the annotation in place, Jackson skips any fields that are null. The resulting JSON would be clean and focused: {"name": "Alejo"}—no unnecessary null cluttering up the payload.

Key Benefits for DTOs

  • Smaller payloads: Excluding nulls reduces the size of your JSON responses/requests, which boosts performance—especially for high-traffic APIs.
  • Less client-side confusion: Frontend apps or other services consuming your API won't have to handle unnecessary null values. They can safely assume a field exists only if it has meaningful data.
  • Aligns with optional fields: If a field like email is optional in your business logic, it makes sense to only send it when it's actually provided.

Other Useful Include Values

Beyond NON_NULL, there are a few other common rules you might want to use:

  • NON_EMPTY: Excludes fields that are null or empty (like empty strings, empty lists, or empty maps). Perfect for fields that shouldn't be sent if they have no content.
  • NON_DEFAULT: Excludes fields set to their default value (e.g., 0 for integers, false for booleans).
  • CUSTOM: Lets you define a custom filter class to decide exactly which fields to include/exclude based on your own logic.

Quick Side Note on Deserialization

While you're using this with @RequestBody (deserializing incoming JSON to a Greeting object), @JsonInclude primarily affects serialization (when you send JSON out from your API). But it still keeps your DTO consistent—if you ever return the Greeting object as a response, it'll be clean and free of unnecessary nulls.

内容的提问来源于stack exchange,提问作者AlejoDev

火山引擎 最新活动