简体   繁体   中英

Orika Object mapper not mapping Lists

I am using an object mapper called Orika, and I have some doubts regarding how it works or am I doing anything wrong here.

Following are the classes

  1. https://googleapis.dev/java/google-cloud-channel/latest/com/google/cloud/channel/v1/Entitlement.html
    This is the source class structure, an object comes in this format.

  2. Entitlements.java

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Entitlement {
    
    private String name;
    private String createTime;
    private String updateTime;
    private String offer;
    private String provisioningState;
    private ProvisionedService provisionedService;
    private String purchaseOrderId;
    private List<String> suspensionReasons;
    private CommitmentSettings commitmentSettings;
    private TrialSettings trialSettings;
    private AssociationInfo associationInfo;
    private List<Parameter> parameters;
}

Parameter.java

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Parameter {
    private String name;
    private Value value;
    private boolean editable;
}
  1. My orika mapper code:
public class OrikaMapper {
    private MapperFactory mapperFactory;
    public <S, D> D map(S s, Class<D> type) {
        return this.mapperFactory.getMapperFacade().map(s, type);
    }
}
  1. my method:
public Entitlement getEntitlement(String credential, String customerId, String entitlementId) {
         CloudChannelServiceClient client = null;
         Entitlement resp = null;
        try {
            client = <<>>;
            String name = "accounts/"+ ACCOUNT_ID +"/customers/"+customerId+"/entitlements/"+entitlementId;
            GetEntitlementRequest request = GetEntitlementRequest.newBuilder().setName(name).build();
            com.google.cloud.channel.v1.Entitlement entitlement = client.getEntitlement(request);
            resp = orikaMapper.map(gson.toJson(entitlement), Entitlement.class);
        } catch (Exception e) {
            throw new ApplicationException("code", e.getMessage());
        }
        return resp;
      }
  1. sample response from google API
{ "name_": "<<>>", "createTime_": { "seconds_": 1615715413, "nanos_": 810000000, "memoizedIsInitialized": -1, "unknownFields": { "fields": {}, "fieldsDescending": {} }, "memoizedSize": -1, "memoizedHashCode": 0 }, "offer_": "<<>>", "provisioningState_": 5, "provisionedService_": { "provisioningId_": "<<>>", "productId_": "<<>>", "skuId_": "<<>>", "memoizedIsInitialized": -1, "unknownFields": { "fields": {}, "fieldsDescending": {} }, "memoizedSize": -1, "memoizedHashCode": 0 }, "suspensionReasons_": [ 4 ], "suspensionReasonsMemoizedSerializedSize": 0, "purchaseOrderId_": "A codelab test", "trialSettings_": { "trial_": false, "memoizedIsInitialized": -1, "unknownFields": { "fields": {}, "fieldsDescending": {} }, "memoizedSize": -1, "memoizedHashCode": 0 }, "parameters_": [ { "name_": "max_units", "value_": { "kindCase_": 1, "kind_": 4, "memoizedIsInitialized": -1, "unknownFields": { "fields": {}, "fieldsDescending": {} }, "memoizedSize": -1, "memoizedHashCode": 0 }, "editable_": true, "memoizedIsInitialized": -1, "unknownFields": { "fields": {}, "fieldsDescending": {} }, "memoizedSize": -1, "memoizedHashCode": 0 }, { "name_": "assigned_units", "value_": { "kindCase_": 1, "kind_": 1, "memoizedIsInitialized": -1, "unknownFields": { "fields": {}, "fieldsDescending": {} }, "memoizedSize": -1, "memoizedHashCode": 0 }, "editable_": false, "memoizedIsInitialized": -1, "unknownFields": { "fields": {}, "fieldsDescending": {} }, "memoizedSize": -1, "memoizedHashCode": 0 } ], "memoizedIsInitialized": -1, "unknownFields": { "fields": {}, "fieldsDescending": {} }, "memoizedSize": -1, "memoizedHashCode": 0 }
  1. Response after mapping in orika
{ "name": "<<>>", "createTime": "seconds: 1615715413\nnanos: 810000000\n", "updateTime": "", "offer": "<<>>", "provisioningState": "SUSPENDED", "provisionedService": { "provisioningId": "<<>>", "productId": "<<>>", "skuId": "<<>>" }, "purchaseOrderId": "A codelab test", "commitmentSettings": { "startTime": "", "endTime": "", "renewalSettings": { "enableRenewal": false, "resizeUnitCount": false, "paymentPlan": "PAYMENT_PLAN_UNSPECIFIED", "paymentCycle": { "duration": 0, "periodType": "PERIOD_TYPE_UNSPECIFIED" } } }, "trialSettings": { "trial": false, "endTime": "" }, "associationInfo": { "baseEntitlement": "" } }

My issue is, the response is not able to map what is in the List<> fields. All the other fields are mapped. What am i missing here.

Rather late in reply, but if this helps anyone in the future. I was facing the same issue. It seems that you have to explicitly call: mapper.mapAsList Ie in questioner's case: parameters = orikaMapper.mapAsList(entitlement.getParameters(), Parameter.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM