从InputStream / String Olingo4获取Edm

时间:2018-04-19 22:46:17

标签: java rest olingo odata4j

在Olingo 2中,我可以这样做:

Edm metadataEdm = EntityProvider.readMetadata(metadataInputStream, false); //metadataInputStream is java.io.InputStream

根据我在Olingo 4中所读到的,你可以这样做:

ODataClient client = ODataClientFactory.getClient();

            String serviceRoot = "http://services.odata.org/V4/Northwind/Northwind.svc";
            EdmMetadataRequest request
               = client.getRetrieveRequestFactory().getMetadataRequest(serviceRoot);

            ODataRetrieveResponse<Edm> response = request.execute();

但在我的项目中,我只能使用HTTPClient进行任何网络调用 - 这意味着我无法使用ODataClient client

HttpClient我可以获得InputStream。有没有办法(如上面的Olingo 2所示)从Olingo 4中的InputStream获取Edm对象?

1 个答案:

答案 0 :(得分:0)

是的,有一种方法可以使用ODataReader在Olingo 4中执行此操作。它接受方法Edm readMetadata(InputStream input);

中的输入流

您可以通过以下方式修改上述代码,以获得相同的结果。

ODataReader reader = ODataClientFactory.getClient().getReader();

String serviceRoot = "http://services.odata.org/V4/Northwind/Northwind.svc/$metadata";

HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(serviceRoot);

httpGet.addHeader("Accept", "application/xml"); // Looks like version 4.4.0 of OData client only supports xml format for metadata

HttpResponse response = httpClient.execute(httpGet);
Edm edm = reader.readMetadata(response.getEntity().getContent());