为什么@jsoninclude(jsoninclude.include.non_empty)不起作用?

发布于 2025-01-26 15:43:12 字数 910 浏览 3 评论 0原文

我正在尝试将零值设置为JSON有效载荷的空值数据库。这个问题引起了Beagause,我对Social实体字段有独特的约束。 我的要求DTO看起来像:

@Value
@With
@Builder
@Jacksonized
public class AccountCreateRequest {

  @NotBlank
  String firstName;

  String phone;

  @Password
  @NotBlank
  String password;

  @Email(message = "Email is not valid.")
  @NotBlank
  String email;

  @NotBlank
  String role;

  SocialDto social;

}

嵌套的社交dto看起来像

@Value
@Builder
@Jacksonized
public class SocialDto {
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  String telegramId;

  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  String linkedinLink;

  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  String githubLink;
}

JSON示例:

{

...fields...,
social: {
   telegramId: "",
   githubLink: "",
   ...
  }
}

这个JSON对象被社会空字符串进行了测试,并且不会忽略该值。 将注释转移到班级级别 - 对我无济于事。

我该如何解决此问题?

I am trying to set null values to database of empty values from JSON payload. This problem caused beacause I have unique constraints on social entity fields.
I have request DTO that looks like:

@Value
@With
@Builder
@Jacksonized
public class AccountCreateRequest {

  @NotBlank
  String firstName;

  String phone;

  @Password
  @NotBlank
  String password;

  @Email(message = "Email is not valid.")
  @NotBlank
  String email;

  @NotBlank
  String role;

  SocialDto social;

}

Nested social DTO looks like

@Value
@Builder
@Jacksonized
public class SocialDto {
  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  String telegramId;

  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  String linkedinLink;

  @JsonInclude(JsonInclude.Include.NON_EMPTY)
  String githubLink;
}

Json example:

{

...fields...,
social: {
   telegramId: "",
   githubLink: "",
   ...
  }
}

This JSON object is deserialized with social empty strings and doesn't ignore that values.
Moving annotation to the class level - didn't help for me.

How could I fix this problem?

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

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

发布评论

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

评论(1

你穿错了嫁妆 2025-02-02 15:43:12

@jsoninclude(jsoninclude.include.non_empty)不适合将空字符串视为对象避免对象的预定目的。该注释是为了将字段包括在对象中,以json序列化。

您需要添加这样的自定义求职者:如何将空白的JSON字符串值归为java.lang.string?

为了将其与@jacksonized结合起来,该 已经注册了> @jsondeserialize(使用= x.class)在类级别上,您可以执行以下操作:

public class JsonStringDeserializer extends JsonDeserializer<String> {

    @Override
    public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        JsonNode node = jsonParser.readValueAsTree();
        String nodeText = node.asText();
        // todo: isBlank or isEmpty depending on your needs
        if (nodeText.isBlank()) {
            return null;
        }
        return nodeText;
    }
}

并使用以下代码解析JSON对象:

SimpleModule stringModule = new SimpleModule();
stringModule.addDeserializer(String.class, new JsonStringDeserializer());
ObjectMapper jsonObjectMapper = new ObjectMapper();
jsonObjectMapper.registerModule(stringModule);
jsonObjectMapper.readValue(input, modelClass);

或者您可以添加@jsondeserialize(used = jsonstringDeserialializer.class.class))在每个字符串字段上,如果您不控制ObjectMapper,则需要它。

@JsonInclude(JsonInclude.Include.NON_EMPTY) does not work for the intended purpose of treating empty strings as null on object deserialization. This annotation is for including fields in Object to JSON serialization.

You need to add a custom Deserializer like so: How to deserialize a blank JSON string value to null for java.lang.String?

In order to combine this with @Jacksonized, which already registers a @JsonDeserialize(using = x.class) on the class level, you can do the following:

public class JsonStringDeserializer extends JsonDeserializer<String> {

    @Override
    public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        JsonNode node = jsonParser.readValueAsTree();
        String nodeText = node.asText();
        // todo: isBlank or isEmpty depending on your needs
        if (nodeText.isBlank()) {
            return null;
        }
        return nodeText;
    }
}

and parse the JSON object with the following code:

SimpleModule stringModule = new SimpleModule();
stringModule.addDeserializer(String.class, new JsonStringDeserializer());
ObjectMapper jsonObjectMapper = new ObjectMapper();
jsonObjectMapper.registerModule(stringModule);
jsonObjectMapper.readValue(input, modelClass);

Or you can add @JsonDeserialize(using = JsonStringDeserializer.class) on every String field that needs it if you don't control the ObjectMapper.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文