JOOQ+杰克逊由于案例敏感性而无法将JSON转换为Pojo

发布于 2025-01-23 16:36:11 字数 530 浏览 2 评论 0原文

以下代码失败:

    final var account = dc.select(ACCOUNT.asterisk())
            .from(ACCOUNT)
            .limit(1)
            .forJSON().path().withoutArrayWrapper()
            .fetchOneInto(Account.class);

错误消息就像:

由: com.fasterxml.jackson.databind.exc.unrecognized propertyexception: 未识别的字段“ firstName” ...已知属性:...“ firstName” ...

杰克逊似乎遇到问题,将上案例列的第一名映射到Pojo的小写字段“ firstName”(并且在所有其他字段中相似)。我正在使用生成的类。在仍在使用JSON的同时,该如何修复?

JOOQ版本为3.16,数据库为Oracle。

The following code fails:

    final var account = dc.select(ACCOUNT.asterisk())
            .from(ACCOUNT)
            .limit(1)
            .forJSON().path().withoutArrayWrapper()
            .fetchOneInto(Account.class);

The error message is like:

Caused by:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
Unrecognized field "FIRSTNAME" ... known properties: ... "firstname"
...

Jackson seems to have problems mapping the upper case column FIRSTNAME into the pojo's lowercase field "firstname" (and similar for all other fields). I am using the generated classes. How can I fix it while still using json?

The jooq version is 3.16 and the database is oracle.

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

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

发布评论

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

评论(1

迷离° 2025-01-30 16:36:11

将杰克逊映射用例固定

在您的特定查询中,您可以将所有列自动添加到其较低的情况相等的列,例如:

final var account = 
    dc.select(Stream
          .of(ACCOUNT.fields())
          .map(f -> f.as(f.getName().toLowerCase()))
          .toList())
      .from(ACCOUNT)
      .limit(1)
      .forJSON().path().withoutArrayWrapper()
      .fetchOneInto(Account.class);

完全避免使用用例,

但是,考虑到您的特定查询通过JSON序列化,只能从第三方映射器中受益。为什么也不使用JOOQ来做到这一点?

final var account = 
    dc.selectFrom(ACCOUNT)
      .limit(1)
      .fetchOneInto(Account.class);

或者,如果帐户是不变的类(例如Java记录),为什么不使用类型的安全映射:

final var account = 
    dc.selectFrom(ACCOUNT)
      .limit(1)
      .fetchOne(Records.mapping(Account::new));

Fixing the Jackson mapping use-case

In your particular query, you could auto-alias all columns to their lower case equivalent, e.g.:

final var account = 
    dc.select(Stream
          .of(ACCOUNT.fields())
          .map(f -> f.as(f.getName().toLowerCase()))
          .toList())
      .from(ACCOUNT)
      .limit(1)
      .forJSON().path().withoutArrayWrapper()
      .fetchOneInto(Account.class);

Avoiding the use-case entirely

However, given your particular query, I don't really see the point of passing through the JSON serialisation only to benefit from a third party mapper. Why not use jOOQ for that as well?

final var account = 
    dc.selectFrom(ACCOUNT)
      .limit(1)
      .fetchOneInto(Account.class);

Or, if Account is an immutable class (e.g. a Java record), why not use type safe mapping like this:

final var account = 
    dc.selectFrom(ACCOUNT)
      .limit(1)
      .fetchOne(Records.mapping(Account::new));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文