连接到OData4返回406不可接受

zbq4xfa0  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(391)

我正在尝试连接到OData4,但出现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)

下面是我试图用olingojars连接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");

我的实现有什么问题?

nfg76nw0

nfg76nw01#

这是因为服务文档的内容类型是application/xml,而不是“application/json;odata.metadata=完整”。也许你应该加上标题“接受”

相关问题