SoFunction
Updated on 2025-04-05

Ignore a certain attribute in SpringBoot project to return data to the front end

When developing applications using Spring Boot, you often encounter situations where you need to return a DTO (data transfer object) instead of returning an entity class. Sometimes, some fields in the entity class may contain sensitive information or need not be passed to the client. In this case, we need to find a way to ignore these fields and return only the required data.

1. Ignore attributes in entity classes

1、@JsonIgnore

@JsonIgnore annotation is an annotation in the Jackson library that is used to ignore properties or interfaces that are not intended to be passed to the foreground when an entity class returns data to the foreground. When you add @JsonIgnore annotation to a property or method of a Java class, Jackson will ignore this property or method when serializing the object to JSON, that is, this property or method will not appear in the generated JSON string. This is very useful for attributes (such as passwords, sensitive information, etc.) that you don't want to expose to the front end.

For example, in the User class, if you have a password property and you don't want to see this property in the foreground, you can add the @JsonIgnore annotation on the password property.

In addition, there is a similar annotation called @JsonIgnoreProperties, which is a class annotation used to ignore some properties in the java bean during json serialization, and both serialization and deserialization are affected. You can add this annotation on the class and specify a list of attribute names to ignore.

It should be noted that if you are using fastJson instead of Jackson as the JSON processing library, the @JsonIgnore annotation may not take effect. In fastJson, you need to use the @JSONField(serialize = false) annotation to achieve the same effect.

2、@JSONField(serialize = false)

@JSONField annotation is annotation in Alibaba's fastjson library, used to control the conversion between Java objects and JSON strings. It can be used to control some details in the serialization and deserialization process, such as date format, serialization order, field name, etc.

@JSONField annotation can be applied to methods, properties, and parameters in methods. In the JSONField annotation, the name attribute is used to specify the name of the key in the JSON string. For example, @JSONField(name = "id") means mapping the userId field in a Java object to the "id" field in a JSON string.

In addition, the @JSONField annotation has some other commonly used properties, such as:

format: Format used to specify date or number. For example, @JSONField(format = "yyyy-MM-dd HH:mm:ss") means converting the createTime field in a Java object to a JSON string in the specified date format.serialize: Used to specify whether to serialize a field. For example, @JSONField(serialize = false) means that a field is not serialized.ordinal: Used to specify the serialization order, the smaller the value, the more serialized it is first.

By using @JSONField annotation, you can flexibly control the conversion between Java objects and JSON strings, achieving more complex serialization and deserialization requirements.

3、@JsonInclude

@JsonInclude is an annotation in the Jackson library that is used to customize which properties should be included during serialization (i.e. converting Java objects to JSON strings). This annotation can be used in a class, method, or field to define how field values ​​are included.

Jackson provides several different Include strategies:

: Always include attributes, regardless of their value.
.NON_ABSENT: Contains non-absent attributes. This is equivalent to NON_NULL (non-empty) plus non-empty sets, non-empty maps, etc..NON_DEFAULT: Contains attributes whose value is not equal to the default value of the field. This requires attention that for basic data types (such as int, long, etc.), their default values ​​are usually 0 or false, while for wrapper types (such as Integer, Long, etc.), their default values ​​are null.
.NON_EMPTY: For strings, only non-empty strings are included; for collections, arrays, and maps, only non-empty sets are included..NON_NULL: Contains only non-null attributes.
: Use a custom inclusion policy.

For example, if you have a class and you just want to include non-empty fields when serializing, you can do this:

import ;  
@JsonInclude(.NON_NULL)  
public class MyObject {  
    private String name;  
    private Integer age;  
    // getters and setters  
}

In this example, if the value of the name or age field is null, then the fields will not be included when the instance of MyObject is serialized as a JSON string.

This annotation is very useful because it helps you control the size and readability of the generated JSON string, especially if you have a lot of optional fields or nested objects.

2. Ignore the return value of the method in the entity class

@Transient annotation is one of the annotations for package definitions provided by the Java EE specification. The purpose of this annotation is to specify that the property or field is not permanent, that is, to tell MyBatis not to map the field into a column of the database table, that is, to mark fields in the entity class that do not need to be persisted to the database.

The use of @Transient annotation is not limited to properties with @Table annotation on an entity class, it can be used on fields or methods of any entity class. Regardless of whether the entity class has @Table annotation or not, as long as the fields or methods marked by the @Transient annotation will be ignored and no database mapping operations will be performed.

In addition, the @Transient annotation can also be used in Java serialization process to mark fields that do not want to be serialized. When the object is serialized, fields marked as @Transient will be ignored.

In general, the main function of the @Transient annotation is to tell the system which fields or attributes do not need to be processed during the persistence and serialization of Java EE.

@Data
public class ResourceParam {
    private String field1;
    private String field2;
    private String field3;
    @Transient
    public String getTest() {
        return “string”;
    }
}

Summarize

In Spring Boot, we can use a variety of methods to ignore fields in the returned data. Whether using @JsonIgnore annotation, Projection projection, @JsonIgnoreProperties annotation, or custom serializer, we can achieve our goals.

In practical applications, choose appropriate methods according to specific scenarios and needs. Remember that protecting sensitive information and reducing data transmission is important. By ignoring unnecessary fields, we can improve system performance and security.