Search code examples
jsonodataodata4j

How do I request JSON data from a OData service operation using OData4J?


I am trying to query a WCF OData service from Java using the odata4j library. If I query a entity using ATOM format, everything is OK. If I switch to JSON, Java throws following exception:

java.lang.RuntimeException: java.lang.RuntimeException: EdmEntitySet GetStatesViewData?gac=0 not found
    at org.core4j.ReadOnlyIterator.hasNext(ReadOnlyIterator.java:49)
    at org.core4j.Enumerable$FuncIterator.hasNext(Enumerable.java:487)
    at org.core4j.Enumerable$FuncIterator.hasNext(Enumerable.java:487)
    at ... Caused by: java.lang.RuntimeException: EdmEntitySet GetStatesViewData?gac=0 not found at org.odata4j.edm.EdmDataServices.getEdmEntitySet(EdmDataServices.java:40)
at org.odata4j.internal.EdmDataServicesDecorator.getEdmEntitySet(EdmDataServicesDecorator.java:31)
at org.odata4j.format.json.JsonFeedFormatParser.parse(JsonFeedFormatParser.java:99)
at org.odata4j.format.json.JsonFeedFormatParser.parse(JsonFeedFormatParser.java:21)
at org.odata4j.consumer.OQueryRequestImpl$EntryIterator.advance(OQueryRequestImpl.java:192)
at org.core4j.ReadOnlyIterator.hasNext(ReadOnlyIterator.java:47)
... 10 more

The following code produced this exception:ODataConsumer odc =

ODataConsumer.create(FormatType.JSON, "http://localhost:6000/");
        Iterator<OEntity> iterator = odc.getEntities("GetStatesViewData?gac=0").execute();
for(OEntity entity: entities)
{
..
}

OData4j is not able to find an entity "GetStatesViewData?gac=0", but such a property does not exist. What exists is a service operation "GetStatesViewData" which accepts a parameter "gac". Is it possible to call a service operation with parameters using OData4j?

If I use Fiddler or some other web debugger, send a HTTP request with a "accept: application/json" header to this OData Service, I normally get the JSON feed. So there is something wrong either in OData4j or in my code, the OData server seems to be OK.


Solution

  • Currently (as of version 0.5) - you can use callFunction to call a service operation.

    See FunctionTest.java on the odata4j Github repo for an example.

    Hope that helps,
    - john