Java Gson exclude fields using @Expose annotation

Tags: JSON gson Gson toJson GsonBuilder excludeFieldsWithoutExposeAnnotation Gson exclude fields

In this Java Gson tutorial we learn how to exclude fields during serialization or deserialize JSON to exclude fields based on @Expose annotation. Via step by step tutorial we will show you how to use the GsonBuilder.excludeFieldsWithoutExposeAnnotation() method to configure Gson to exclude fields without @Expose annotation.

How to add Gson to the Java project

To use the Gson library in the Gradle build project, add the following dependency into the build.gradle file.

implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.7'

To use the Gson library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.7</version>
</dependency>

Or you can download the Gson jar file from Maven Central at gson-2.8.7.jar

To have more information about the Gson library you can visit the project repository at github.com/google/gson

How to exclude fields using @Expose annotation

For example, we have the Product class as below.

Product.java

import com.google.gson.annotations.Expose;

public class Product {
    @Expose
    public Integer id;

    @Expose
    public String productName;

    @Expose
    public Double price;

    public String category;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public Double getPrice() {
        return price;
    }

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

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }
}

As you can see we have added @Expose annotation to id, productName, price fields meaning that these fields will be included during serialization or deserialize JSON and exclude category field as it does not @Expose annotation.

In the following Java example program we show you how to use GsonBuilder.excludeFieldsWithoutExposeAnnotation() method to configure Gson to exclude fields without @Expose annotation to convert Product object to JSON String.

ExcludeFieldsWithoutExposeAnnotationExample.java

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class ExcludeFieldsWithoutExposeAnnotationExample {
    public static void main(String... args) {
        Product product = new Product();
        product.setId(999);
        product.setProductName("Phone");
        product.setPrice(250.10);
        product.setCategory("Electronic devices");

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder
                .excludeFieldsWithoutExposeAnnotation()
                .create();
        String jsonString = gson.toJson(product);

        System.out.println("Output JSON: ");
        System.out.println(jsonString);
    }
}
The output is:
Output JSON: 
{"id":999,"productName":"Phone","price":250.1}

Happy Coding 😊