CustomQueryParam java sdk not give actual response as Rest api

Product/components used and version/fix level:

1017.73.0

Detailed explanation of the problem:

I have API <url>/managedObjects?type=sol_AlarmFlow&deviceId=1942089 ,getting correct response in postman with get like

{
    "next": "https://<url>/inventory/managedObjects?pageSize=5&type=sol_AlarmFlow&currentPage=2&deviceId=1942089",
    "self": "https://<url>/inventory/managedObjects?pageSize=5&type=sol_AlarmFlow&currentPage=1&deviceId=1942089",
    "managedObjects": [
        {
            "additionParents": {
                "references": [],
                "self": "https://<url>/inventory/managedObjects/55328249458/additionParents"
            },
            "owner": "",
          
            "alarmFlows": [
                {
                    "email_to": "",
                    "alarm_flow_name": "First Alarm flow 05dec23",
                
                }
            ]
        }
    ],
    "statistics": {
        "pageSize": 5,
        "currentPage": 1
    }
}

But Same thing applied using Java sdk while following Best-Practices to use the Microservice SDK for Java for custom API queries not able to get correct response here is code

CustomQueryParam.java

public enum CustomQueryParam implements Param {
	WITH_TOTAL_PAGES("withTotalPages"),
	PAGE_SIZE("pageSize"),
	QUERY("query"),
	DEVICE_QUERY("q"),
	DATE_FROM("dateFrom"),
	STATUS("status"),
	FRAGMENT_TYPE("fragmentType"),
	DEVICE_ID("deviceId"),
	REVERT("revert"),
	TYPE("type")
	;

	private String name;
	
	private String value;
	
	public String getValue() {
		return value;
	}

	private CustomQueryParam(final String name) {
		this.name = name;
	}

	@Override
	public String getName() {
		return name;
	}

	public CustomQueryParam setValue(final String value) {
		this.value = value;
		return this;
	}

	public QueryParam toQueryParam() {
		return new QueryParam(this, Filter.encode(value));
	}

}

Implentation class

	int pageSize = 5000;
			            InventoryFilter deviceFilter = new InventoryFilter().byType("sol_AlarmFlow");

						QueryParam creationDeviceQuery = CustomQueryParam.DEVICE_ID.setValue("1942089").toQueryParam();
						
						Iterable<ManagedObjectRepresentation> managedObjects = inventory
								.getManagedObjectsByFilter(deviceFilter)
								.get(pageSize, creationDeviceQuery)
								.allPages();
						while (managedObjects.iterator().hasNext()) {
					        ManagedObjectRepresentation managedObjectRepresentation = managedObjects.iterator().next();
					        try {
					        	
					        	LOG.info(String.format("Fteched managed object with id %s from tenant %s", 
										managedObjectRepresentation.getId().getValue(), 
										
										new ObjectMapper().writeValueAsString(managedObjectRepresentation)));
							} catch (JsonProcessingException e) {
								LOG.error("Error writing JSON string", e);
							}
					    }

its always giving this response not where manageObject should come

{
    "owner": "",
    "additionParents": {
        "references": [],
        "self": "https://<url>/inventory/managedObjects/55328249458/additionParents"
    },
    "childDevices": {
        "references": [],
        "self": "https://<url>/inventory/managedObjects/55328249458/childDevices"
    },
    "childAssets": {
        "references": [],
        "self": "https://<url>/inventory/managedObjects/55328249458/childAssets"
    },
    "creationTime": "2023-12-05T16:02:06.801Z",
    "type": "sol_AlarmFlow",
    "lastUpdated": "2023-12-05T16:02:07.136Z",
    "childAdditions": {
        "references": [],
        "self": "https://<url>/inventory/managedObjects/55328249458/childAdditions"
    },
    "deviceParents": {
        "references": [],
        "self": "https://<url>/inventory/managedObjects/55328249458/deviceParents"
    },
    "assetParents": {
        "references": [],
        "self": "https://<url>/inventory/managedObjects/55328249458/assetParents"
    },
    "id": "55328249458",
   
    "alarmFlows": [
        {
            "email_to": "",
           }
    ]
}
1 Like

It is a bit hard to read what the problem is due the formatting problems in your mail.

Is the problem that instead of getting the managed object array you get individual managed objects? That is to be expected as you are iterating of the collection and logging the individual objects.

I don’t understand the issue. You get a collection back which returns a PagedManagedObjectCollectionRepresentartion which is actually the response you expect on top of your post by just using get(...). If you want to print this out it would be the same response as in postman.
You skip that by adding allPages() which makes an iterable out of the Collection.

Then you are iterating over the collection and print out all single managed objects.

1 Like

@Stefan_Witschel thanks for the reply ,after removing allPages() i get the response which matches with postman.

thanks for reply,yes there was iteration issue in code snippet after removing allPages() it works

1 Like