如何将 CouchDB 文档上的属性映射到 java 模型对象?

发布于 2025-01-11 22:20:27 字数 708 浏览 0 评论 0原文

我在 CouchDB 的数据库中有一组文档,其结构如下:

    {
      "_id": "adebafb022857385a4c970b0f0000349",
      "_rev": "2-19d78f4d8d6386bfbe7b2c03b5213830",
      "street_number": "20",
      "street_name": "B.Baker St",
      "city_name": "Marylebone",
      "zip_code": "2550",
      "status": "active"
    }

如您所见,属性不是驼峰式大小写的,我尝试使用 JsonProperty 进行映射,但是,我在所有属性中都得到空值,除了对于状态属性。

输入图片此处描述

我已将代码推送到此存储库中链接

非常感谢任何帮助

关于 CouchDB,我使用带有 CouchDB 映像的 docker 容器。

I have within a database a set of documents in CouchDB where their structure is as follow :

    {
      "_id": "adebafb022857385a4c970b0f0000349",
      "_rev": "2-19d78f4d8d6386bfbe7b2c03b5213830",
      "street_number": "20",
      "street_name": "B.Baker St",
      "city_name": "Marylebone",
      "zip_code": "2550",
      "status": "active"
    }

as you can see the properties are not camel-cased, I have tried to make the mapping using JsonProperty, however, I get null value in all properties except for status property.

enter image description here

I have pushed the code in this repo link

Any help is much appreciated

Regarding the CouchDB, I using a docker container with CouchDB image.

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

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

发布评论

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

评论(2

胡渣熟男 2025-01-18 22:20:27

假设这与 https://github.com/cloudant/java-cloudant/ 相同issues/536 值未正确反序列化,因为 JsonPropertyJackson 注释,但库正在使用 Gson反序列化。

Gson 等效注释是@SerializedName

Assuming this is the same as https://github.com/cloudant/java-cloudant/issues/536 the values are not deserialized correctly because JsonProperty is a Jackson annotation, but the library is using Gson deserialization.

The Gson equivalent annotation is @SerializedName.

已下线请稍等 2025-01-18 22:20:27

我已将 com.google.code.gson 依赖项与 @SerializedName 一起使用,如下所示:

package com.example.couch_mapping.model;

import com.google.gson.annotations.*;
import lombok.Data;

@Data
public class AddressDto {
    @SerializedName("_id")
    String id;

    @SerializedName("_rev")
    String revision;

    @SerializedName("street_number")
    String streetNumber;

    @SerializedName("street_name")
    String streetName;

    @SerializedName("city_name")
    String cityName;

    @SerializedName("zip_code")
    String zipCode;

    String status;
}

I have used com.google.code.gson dependency with @SerializedName as follows:

package com.example.couch_mapping.model;

import com.google.gson.annotations.*;
import lombok.Data;

@Data
public class AddressDto {
    @SerializedName("_id")
    String id;

    @SerializedName("_rev")
    String revision;

    @SerializedName("street_number")
    String streetNumber;

    @SerializedName("street_name")
    String streetName;

    @SerializedName("city_name")
    String cityName;

    @SerializedName("zip_code")
    String zipCode;

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