user3423878
user3423878

Reputation: 51

Navigation links are not returned when using JSON for OData (using Apache Olingo for Java)

when using the below code using XML it works perfectly, that is, the navigation links are returned successfully. However, when I change the format to "json" or "application/json", the links2 navigation links list is empty, meaning that rental.getNavigations() returns an empty list.

Can anyone help please? I am using Apache Olingo for Java OData v4.

Thanks

URI uri = client.newURIBuilder(serviceRoot)
                 .appendEntitySetSegment("Rentals")
                 .appendKeySegment(1).format("application/xml").build();

ODataRetrieveResponse<ODataEntity> response2 = client.getRetrieveRequestFactory().getEntityRequest(uri).execute();
ODataEntity rental = response2.getBody();

        List<ODataLink> links2 = rental.getNavigationLinks();
        for (ODataLink link : links2) {
            System.out.println(link.getRel());
            System.out.println(link.getName());
            URI linkUri = client.newURIBuilder(serviceRoot)
                      .appendNavigationSegment(link.getLink().toString()).format("atom").build();
            ODataRetrieveResponse<ODataEntity> responseCustomer
                    = client.getRetrieveRequestFactory().getEntityRequest(linkUri).execute();
            ODataEntity cust = responseCustomer.getBody();
            if(link.getName().equals("Stock"))
                System.out.println(cust.getProperty("Status").getValue().toString()); 
            else System.out.println(cust.getProperty("Name").getValue().toString()); 
       } 

Upvotes: 0

Views: 848

Answers (1)

lencharest
lencharest

Reputation: 2935

The odata.metadata=full format parameter is necessary to get odata.navigationLink properties to appear in the JSON response. Add odata.metadata=full to the format option when building the client. The full format should be application/json;odata.metadata=full. If you have access to the request headers via the client object, you might consider setting the Accept header instead.

Upvotes: 1

Related Questions