cloudant / java-cloudant

A Java client for Cloudant
Apache License 2.0
79 stars 68 forks source link

How to map properties on the CouchDB document to java model object? #536

Closed azdoudYoussef closed 2 years ago

azdoudYoussef commented 2 years ago

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,

@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class AddressDto {
    @JsonProperty("_id")
    String id;

    @JsonProperty("_rev")
    String revision;

    @JsonProperty("street_number")
    String streetNumber;

    @JsonProperty("street_name")
    String streetName;

    @JsonProperty("city_name")
    String cityName;

    @JsonProperty("zip_code")
    String zipCode;

    String status;
}

however, I get null value in all properties except for status property.

image

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.

Versions: java : 11 cloudand : 2.19.1 spring boot : 2.6.4

ricellis commented 2 years ago

This library is no longer maintained.

The problem you are seeing is because JsonProperty is a Jackson annotation; this library is Gson based (see e.g. https://github.com/cloudant/java-cloudant/issues/352).

If for whatever reason you want to continue using/forking this now unmaintained library you would need to change your model class to use Gson e.g. by using the equivalent annotation (@SerializedName), or register a GsonBuilder with a TypeAdapter that supports deserialization into your model class.

Alternatively our replacement https://github.com/IBM/cloudant-java-sdk/ has an option to get raw byte streams which you can use to bypass its built-in serializer and pass the bytes to an alternative serializer of your choice (e.g. Jackson).