是否有杰克逊注释在避难化期间以及在字符串序列化过程中使用包装类课程

发布于 2025-01-19 02:17:49 字数 913 浏览 0 评论 0原文

您好 StackOverflow 社区,

我目前正在尝试反序列化通过 Spring Boot @RestController 提供的 JSON 请求主体。

请求正文包含以下数组:

{
  ...
  "productIds": [
    "123abc",
    "234def"
  ],
  ...
}

但是,我不想将产品 ID 反序列化为字符串列表,而是使用简单的包装类(出于各种原因,包括但不限于额外的类型安全和验证机会) 。因此,该类看起来像这样(Lombok 注释用于保持代码片段简短):

@Value
@AllArgsConstructor
public class TheRequest {
   ...
   List<ProductId> productIds;
   ...
}

,ProductId 只是一个简单的包装器(为了简洁起见,省略了验证注释):

@Value
@AllArgsConstructor
public class ProductId{
   String id;
}

如前所述 Stackoverflow 我只找到了使用相当冗长的自定义反序列化方法来实现此目的的方法。 然而,令我有点惊讶的是,Jackson 并没有提供开箱即用的功能。 那就太好了

  • 因此,如果有人知道是否有一种更优雅的方法来实现将字符串数组反序列化为 WrapperObjects 列表(最好仅使用 Jackson 注释),
  • ?有没有一种优雅的方法来实现将这样的 ProductId 包装对象列表序列化回 String 对象,理想情况下也只使用 Jackson 注释?我尝试了 Jacksons @Value 但没有提供所需的结果。

Hi StackOverflow Community,

I am currently trying to deserialize JSON request bodies provided via Spring Boot @RestController.

The request body contains the following array:

{
  ...
  "productIds": [
    "123abc",
    "234def"
  ],
  ...
}

However, I don't want to deserialize the product IDs into a list of Strings, but rather use a simple wrapper class (for various reasons, including but not limited to additional type safety and validation opportunities). Consequently the class looks like this (Lombok annotations were used to keep the code snippet short):

@Value
@AllArgsConstructor
public class TheRequest {
   ...
   List<ProductId> productIds;
   ...
}

with ProductId being just a simple wrapper as already said (validation annotations are omitted for the sake of brevity):

@Value
@AllArgsConstructor
public class ProductId{
   String id;
}

Looking at Stackoverflow I only found ways to achieve this using rather verbose custom deserialization methods.
However, I am a bit astonished, that Jackson does not provide this functionality out of the box. Consequently it would be great if anyone has any idea if

  • there is a more elegant way to achieve deserialization of a array of Strings into a List of WrapperObjects, ideally only using Jackson annotations?
  • there is an elegant way to achieve serialization of such a resulting List of ProductId wrapper objects back into String objects, ideally also using only Jackson annotations? I tried Jacksons @Value but that did not provide the required result.

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

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

发布评论

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

评论(1

流绪微梦 2025-01-26 02:17:49

对我来说仍然很冗长,但它似乎是 Jacson 2.14+ 的一个有效解决方案:

public record PayloadId(String id) {

  @JsonCreator(mode = Mode.DELEGATING)
  public PayloadId{}

  @JsonValue
  @Override
  public String id() {
    return id;
  }
}

...这是记录测试 https://github.com/FasterXML/jackson-databind/blob/2.14/src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordCreatorsTest.java

To me still to verbose but it seems to be a working solution with Jacson 2.14+:

public record PayloadId(String id) {

  @JsonCreator(mode = Mode.DELEGATING)
  public PayloadId{}

  @JsonValue
  @Override
  public String id() {
    return id;
  }
}

...and here is the records test https://github.com/FasterXML/jackson-databind/blob/2.14/src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordCreatorsTest.java

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