简体   繁体   中英

Odata ClientEntity Error - Expected OData Entity, found EntitySet

I'm trying to use Olingo to connect to an Odata v4 service I've tested it with the Northwind sample and the URI that gets produce works in a browser: I always get the following

error:java.lang.IllegalArgumentException:org.apache.olingo.commons.api.serialization.ODataDeserializerException:
com.fasterxml.jackson.core.JsonParseException: Expected OData Entity, found
EntitySet.

Code

ODataEntityRequest<ClientEntity> reqEntity = client.getRetrieveRequestFactory().getEntityRequest(entityURI);
                ODataRetrieveResponse<ClientEntity> entity = reqEntity.execute();
            ClientEntity ce = (ClientEntity)entity.getBody();

at last line i am getting error.

ClientEntity ce = (ClientEntity)entity.getBody();

This error means that you are querying a collection URL like http://services.odata.org/V4/Northwind/Northwind.svc/Categories . The response for this is the EntitySet .

However, seems that you want a single Entity . It can be got from the URL like http://services.odata.org/V4/Northwind/Northwind.svc/Categories(1) , where (1) corresponds with the ID of the Category you want to get.

2 options:

  • If you want a single Entity , create a proper URI, eg ODataClientFactory.getClient().newURIBuilder("http://services.odata.org/V4/Northwind/Northwind.svc").appendEntitySetSegment("Categories").appendKeySegment(1).build() and execute a request on this (I think you can even skip the casting thing)
  • If you want EntitySet , go for getEntitySetRequest against your uri and then use getBody().getEntities() on the result of the execute() method

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