使用自定义注释将空字符串转换为null

发布于 2025-02-02 22:11:32 字数 260 浏览 1 评论 0原文

我想注释字符串字段。例如:

@EmptyToNull
String name

我不想验证该领域。在发送DTO中的字符串的空值时,我想将空白转换为null。也许杰克逊数据处理器有一个解决方案?例如:

@JsonSetter(nulls = Nulls.AS_EMPTY) *changes null to empty*

我需要带有逻辑的注释,但在任何地方都找不到示例。有人帮忙吗?

I would like to annotate the String field. E.g:

@EmptyToNull
String name

I don't want to validate the field. I would like to convert blanks to null when sending an empty value for a String in DTO. Maybe there is a solution with Jackson Data Processor? E.g:

@JsonSetter(nulls = Nulls.AS_EMPTY) *changes null to empty*

I need an annotation with logic, but can't find an example anywhere. Someone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

來不及說愛妳 2025-02-09 22:11:32

Deserialization

Extend StringDeserializer

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StringDeserializer;

import java.io.IOException;

public class EmptyToNull extends StringDeserializer {
    @Override
    public String deserialize(JsonParser parser, DeserializationContext ctx) throws IOException {
        String result = super.deserialize(parser, ctx);
        if (result != null && result.isEmpty()) {
            return null;
        }
        return result;
    }
}

Use EmptyToNull deserializer for specific field. It will look like custom annotation

public class MyClass {
    public String title;
    @JsonDeserialize(using = EmptyToNull.class)
    public String name;
}

Also, you can register the deserializer globally for all strings for ObjectMapper

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new SimpleModule().addDeserializer(String.class, new EmptyToNull()));

Alternative solution

Implement logic in constructor

public class MyClass {
    private String title;
    private String name;

    @JsonCreator
    public MyClass(@JsonProperty("title") String title,@JsonProperty("name") String name) {
        this.title = title;
        this.name = name!= null && name.isEmpty() ? null : name;
    }
}

Or create custom setter for field

public class MyClass {
    private String title;
    private String name;

    @JsonSetter
    public void setName(String name) {
        this.name = name != null && name.isEmpty() ? null : name;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

Serialization

Jackson provides out of box annotation

public class MyClass {
    public String title;
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    public String name;
}

Deserialization

Extend StringDeserializer

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StringDeserializer;

import java.io.IOException;

public class EmptyToNull extends StringDeserializer {
    @Override
    public String deserialize(JsonParser parser, DeserializationContext ctx) throws IOException {
        String result = super.deserialize(parser, ctx);
        if (result != null && result.isEmpty()) {
            return null;
        }
        return result;
    }
}

Use EmptyToNull deserializer for specific field. It will look like custom annotation

public class MyClass {
    public String title;
    @JsonDeserialize(using = EmptyToNull.class)
    public String name;
}

Also, you can register the deserializer globally for all strings for ObjectMapper

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new SimpleModule().addDeserializer(String.class, new EmptyToNull()));

Alternative solution

Implement logic in constructor

public class MyClass {
    private String title;
    private String name;

    @JsonCreator
    public MyClass(@JsonProperty("title") String title,@JsonProperty("name") String name) {
        this.title = title;
        this.name = name!= null && name.isEmpty() ? null : name;
    }
}

Or create custom setter for field

public class MyClass {
    private String title;
    private String name;

    @JsonSetter
    public void setName(String name) {
        this.name = name != null && name.isEmpty() ? null : name;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

Serialization

Jackson provides out of box annotation @JsonInclude(JsonInclude.Include.NON_EMPTY) to skip empty strings in case serialization.

public class MyClass {
    public String title;
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    public String name;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文