@JsonInclude এর মাধ্যমে null এবং empty values exclude করা

@JsonIgnore এবং @JsonInclude - জ্যাকসন অ্যানোটেশন (Jackson Annotations) - Java Technologies

279

@JsonInclude অ্যানোটেশনটি Jackson-এ JSON serialization-এর সময় নির্দিষ্ট শর্ত অনুযায়ী ফিল্ডগুলোকে অন্তর্ভুক্ত বা বাদ দিতে ব্যবহৃত হয়। এটি null, empty, বা default values-এর মতো অপ্রয়োজনীয় ডেটা JSON আউটপুট থেকে বাদ দিতে সাহায্য করে।


1. @JsonInclude Overview

@JsonInclude কাস্টমাইজেশন করার জন্য অনেক অপশন প্রদান করে। এই অ্যানোটেশনটি ক্লাস বা ফিল্ডের উপর প্রয়োগ করা যেতে পারে।

Syntax:

@JsonInclude(JsonInclude.Include.VALUE)

Common Include Values:

  1. Include.ALWAYS (Default): সব ফিল্ড অন্তর্ভুক্ত করে।
  2. Include.NON_NULL: শুধুমাত্র non-null ফিল্ড অন্তর্ভুক্ত করে।
  3. Include.NON_EMPTY: null এবং empty (যেমন: empty string, collection) বাদ দেয়।
  4. Include.NON_DEFAULT: ফিল্ডের default value বাদ দেয়।
  5. Include.CUSTOM: কাস্টম লজিকের মাধ্যমে serialization নিয়ন্ত্রণ করে।

2. Maven Dependency

Ensure your project has the Jackson dependency in the pom.xml:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.2</version>
</dependency>

3. Practical Examples of @JsonInclude

Example 1: Exclude Null Values

import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class User {
    private String name;
    private String email;

    // Getters and Setters
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

Test Serialization:

import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    public static void main(String[] args) throws Exception {
        User user = new User();
        user.setName("Alice");
        // Email is null

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(user);
        System.out.println(json);
    }
}

Output:

{
  "name": "Alice"
}

Example 2: Exclude Null and Empty Values

import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class User {
    private String name;
    private String email;
    private String phoneNumber;

    // Getters and Setters
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
}

Test Serialization:

import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    public static void main(String[] args) throws Exception {
        User user = new User();
        user.setName("Alice");
        user.setEmail(""); // Empty String
        // Phone Number is null

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(user);
        System.out.println(json);
    }
}

Output:

{
  "name": "Alice"
}

Example 3: Exclude Default Values

import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class Product {
    private String name;
    private double price = 0.0; // Default value

    // Getters and Setters
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

Test Serialization:

import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    public static void main(String[] args) throws Exception {
        Product product = new Product();
        product.setName("Laptop");
        // Price is default (0.0)

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(product);
        System.out.println(json);
    }
}

Output:

{
  "name": "Laptop"
}

4. Applying @JsonInclude at Class and Field Level

Field-Level Application:

public class User {
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String name;

    private String email;

    // Getters and Setters
}

Class-Level Application:

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class User {
    private String name;
    private String email;

    // Getters and Setters
}

5. Combine @JsonInclude with @JsonProperty

You can combine @JsonInclude with @JsonProperty for fine-grained control.

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class User {
    @JsonProperty("full_name")
    private String name;

    @JsonProperty("user_email")
    private String email;

    // Getters and Setters
}

Output:

{
  "full_name": "Alice"
}

6. Exclude Null and Empty Globally

Instead of using @JsonInclude on each class, configure it globally using ObjectMapper.

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

        User user = new User();
        user.setName("Alice");
        user.setEmail(""); // Empty String

        String json = mapper.writeValueAsString(user);
        System.out.println(json);
    }
}

Output:

{
  "name": "Alice"
}

7. Key Points

  • NON_NULL: Null values বাদ দেয়।
  • NON_EMPTY: Null এবং empty string/collection বাদ দেয়।
  • NON_DEFAULT: Default values বাদ দেয়।
  • Globally ObjectMapper-এ configuration করে প্রজেক্ট জুড়ে নিয়ন্ত্রণ করা যায়।

  • @JsonInclude JSON serialization নিয়ন্ত্রণের জন্য একটি শক্তিশালী টুল।
  • Null এবং empty values বাদ দিয়ে JSON ডেটা আরও পরিষ্কার ও সংক্ষিপ্ত করা যায়।
  • Field বা Class লেভেলে ব্যবহার করে কাস্টমাইজেশনের সুবিধা পাওয়া যায়।
  • Globally configuration করে পুরো প্রজেক্টে একই নিয়ম প্রয়োগ করা সম্ভব।
Content added By
Promotion

Are you sure to start over?

Loading...