In one of my blog about C# call s4hc  odata api(https://s4hanacloud.community.sap/blogs/show/oRwgFa9Pxe3dEC1C74Tii5),  C# can generate  class for s4hc odata api .  It is easy to  use  the related class to CRUD  operation through S4HC odata API .  Then is it possible to do the same in JAVA ?  The answer is yes,  if  develop in SCP , use  cloud platform sdk . If develop  in third party java system, use API Hub sdk . Today's blog is hwo to use  API hub  SDK to Call odata api in s4hc .  

Step 1, download  API hub SDK  , still I will use billing odata api .

Step 2,  Import project in Eclipse 

Paste the  source code , change the URL , user name and pass word .

The following is the source code :

package com.sap.apibhub.sdk.consumer;

import com.sap.apibhub.sdk.client.*;

import com.sap.apibhub.sdk.client.auth.ApiKeyAuth;

//import com.squareup.okhttp.Response;

import com.sap.apibhub.sdk.client.auth.HttpBasicAuth;

import com.sap.apibhub.sdk.client.auth.OAuth;

import java.util.ArrayList;

import com.sap.apibhub.sdk.api_billing_document_srv.api.*;

import com.sap.apibhub.sdk.api_billing_document_srv.model.ABillingDocumentType;

public class BillingService {

public static void main(String[] args) 

{

ApiClient defaultApiClient = Configuration.getDefaultApiClient();

defaultApiClient.setBasePath("https://my3xxxxx-api.saps4hanacloud.cn/sap/opu/odata/sap/API_BILLING_DOCUMENT_SRV");

defaultApiClient.getAuthentications().clear();

defaultApiClient.getAuthentications().put("httpBaseAuth", new HttpBasicAuth());

defaultApiClient.setUsername("username");

defaultApiClient.setPassword("password");

defaultApiClient.setConnectTimeout(2000);

ABillingDocumentApi abillingDoc = new ABillingDocumentApi();

abillingDoc.setApiClient(defaultApiClient);

// for(Authentication: auth in defaultApiClient.getAuthentications() ) {}

try { 

ArrayList<String> filter = new ArrayList<String>();

ArrayList<String> expand = new ArrayList<String>();

filter.add("BillingDocument");

filter.add("CreationDate");

filter.add("CreationTime");

expand.add("to_Item");

expand.add("to_Partner");

ABillingDocumentType  result = abillingDoc.aBillingDocumentBillingDocumentGet("90000000",filter,expand);

   System.out.println(result.toString());

}

catch(ApiException e){

System.err.println(e.getResponseBody());

}

finally {

System.out.println("odata call finished");

}

}

}