连接到Odata 4将返回406 Not Acceptable

时间:2015-01-15 11:07:51

标签: java web-services rest odata

我正在尝试连接到Odata 4,我收到了406错误。

The content-type range '[application/json;odata.metadata=full]' is not supported. [HTTP/1.1 406 Not Acceptable]

完整的堆栈跟踪:

Exception in thread "main" org.apache.olingo.client.api.communication.ODataClientErrorException: The content-type range '[application/json;odata.metadata=full]' is not supported. [HTTP/1.1 406 Not Acceptable]
    at org.apache.olingo.client.core.communication.header.ODataErrorResponseChecker.checkResponse(ODataErrorResponseChecker.java:73)
    at org.apache.olingo.client.core.communication.request.AbstractRequest.checkResponse(AbstractRequest.java:53)
    at org.apache.olingo.client.core.communication.request.AbstractODataRequest.doExecute(AbstractODataRequest.java:324)
    at org.apache.olingo.client.core.communication.request.retrieve.ODataServiceDocumentRequestImpl.execute(ODataServiceDocumentRequestImpl.java:57)
    at org.apache.olingo.client.core.communication.request.retrieve.ODataServiceDocumentRequestImpl.execute(ODataServiceDocumentRequestImpl.java:37)
    at com.acc.odata.OlingoConnect.main(OlingoConnect.java:23)

以下是我尝试使用olingo jars连接odata的代码

String serviceRoot = "http://localhost:8080/odata-server-sample/cars.svc";
ODataClient client = ODataClientFactory.getV4();
ODataServiceDocumentRequest req = client.getRetrieveRequestFactory()
    .getServiceDocumentRequest(serviceRoot);
ODataRetrieveResponse<ODataServiceDocument> res = req.execute();
ODataServiceDocument serviceDocument = res.getBody();
Collection<String> entitySetNames = serviceDocument.getEntitySetNames();
Map<String, URI> entitySets = serviceDocument.getEntitySets();
Map<String, URI> singletons = serviceDocument.getSingletons();
Map<String, URI> functionImports = serviceDocument.getFunctionImports();
URI productsUri = serviceDocument.getEntitySetURI("Cars");

我的实施有什么问题?

1 个答案:

答案 0 :(得分:0)

那是因为服务文档的Content-Type是application / xml,而不是&#34; application / json; odata.metadata = full&#34;。也许你应该添加标题&#34;接受&#34;