You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将响应对象的字符串参数设置为null?赋值null却返回空字符串的解决方法

Why Are String Fields Returning Empty Strings Instead of Null?

Hey Alex, let's break down why your firstname and lastname fields are coming back as empty strings ("") instead of null, even though you're explicitly setting them to null in your service code. The id field working correctly gives us a clue—it's almost certainly related to how your JSON serialization framework (most likely Jackson, since you're using Spring-style DTOs) is handling string values. Here are the top fixes to try:


1. Check for Custom String Serializers

If your project has a custom Jackson StringSerializer that's converting null to empty strings, that's the culprit. Look for code like this somewhere in your configuration:

public class EmptyStringSerializer extends StdSerializer<String> {
    public EmptyStringSerializer() {
        super(String.class);
    }

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        // This line turns null into ""
        gen.writeString(value == null ? "" : value);
    }
}

Fix: Modify the serializer to write null instead of an empty string when the value is null:

@Override
public void serialize(String value, JsonGenerator gen, SerializerProvider provider) throws IOException {
    if (value == null) {
        gen.writeNull();
    } else {
        gen.writeString(value);
    }
}

Or remove the custom serializer entirely if you don't need this behavior.


2. Fix JavaBean Getter/Setter Naming

Notice your response uses firstName and lastName (camelCase), but your DTO fields are firstname and lastname (all lowercase). If your getter/setter methods are named incorrectly (e.g., getFirstName() instead of getFirstname()), Jackson might not properly map the field value, leading to an empty string instead of null.

Fix:

  • Either adjust your getters/setters to match the field name exactly (IDE auto-generate usually does this correctly):
    public String getFirstname() {
        return firstname;
    }
    
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    
  • Or use @JsonProperty to explicitly map the field to the camelCase key in your response:
    public class UserDto {
        Long id;
        
        @JsonProperty("firstName")
        String firstname;
        
        @JsonProperty("lastName")
        String lastname;
        
        // Getters and setters
    }
    

3. Force Jackson to Serialize Null Values

If your global Jackson configuration is set to exclude null values or replace them, you can override this for your DTO with the @JsonInclude annotation:

@JsonInclude(JsonInclude.Include.ALWAYS)
public class UserDto {
    Long id;
    String firstname;
    String lastname;
    // Getters and setters
}

This annotation tells Jackson to serialize every field, regardless of its value—so null values will show up as null in the response instead of being replaced or excluded.


4. Check for FastJSON/Gson Configuration (If Using Alternate Serializers)

If you're using FastJSON instead of Jackson, it defaults to converting null strings to empty strings. Fix this by enabling the WriteNullStringAsNull feature:

// In your service code when serializing
String json = JSON.toJSONString(userDto, SerializerFeature.WriteNullStringAsNull);

Or set it globally in your application.properties:

spring.fastjson.serializer-features=WriteNullStringAsNull

For Gson, make sure you're not using a custom TypeAdapter that modifies null strings, and ensure serializeNulls() is enabled if you want nulls to appear in the response.

Start with checking the serializer configuration and getter/setter names—those are the most common issues here.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 06:43:16