我正在使用olingo使用OData v4协议进行其余服务。当我说:

Company.svc /部门(x)/员工(x)/ BussinesTrips

在Olingo的教程中,有一个示例,其中有2段导航。其中第一段是UriResourceEntitySet,第二段是UriResourceNavigation。
在示例中,这两个(特别是entitySet)在从存储中获取相关实体集合的方法中需要作为参数。

在我的示例中,有Company.svc / UriResourceEntitySet / UriResourceNavigation / UriResourceNavigation,我可以从UriInfo参数中说出什么。

我不知道该怎么做。我应该更改方法还是以某种方式强制将倒数第二段设为EntitySet。

感谢您的参与,即时通讯正在等待回调。

@Override
public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat)
        throws ODataApplicationException, ODataLibraryException {
    String lastUri = "lastUriResource";
    String sourceNavigationUri = "sourceUriResource";

    EdmEntitySet responseEntitySet = null;
    EntityCollection responseEntityCollection = null;

    List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
    UriResource uriResource = resourcePaths.get(0);
    if (!(uriResource instanceof UriResourceEntitySet)) {
        throw new ODataApplicationException("Only EntitySet is supported", HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
    }

    UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) uriResource;
    EdmEntitySet startEntitySet = uriResourceEntitySet.getEntitySet();
    if (resourcePaths.size() == 1) {
        responseEntitySet = startEntitySet;
        responseEntityCollection = storage.readEntitySetData(responseEntitySet);
    } else {
        startEntitySet = Util.getNavigationTargetEntitySet(uriInfo);
        HashMap<String, UriResource> uriResourceHashMap = Util.getLastNavigationAndItsSource(uriInfo);
        UriResource lastUriResource = uriResourceHashMap.get(lastUri);
        UriResource sourceUriResource = uriResourceHashMap.get(sourceNavigationUri);
        EdmNavigationProperty edmNavigationProperty = null;
        if (!(lastUriResource instanceof UriResourceNavigation)) {
            throw new ODataApplicationException("Only navigation is supported", HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
        }
        edmNavigationProperty = ((UriResourceNavigation) lastUriResource).getProperty();

        if (!(sourceUriResource instanceof UriResourceEntitySet)) {
            throw new ODataApplicationException("Only Entity Set is supported", HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
        }
        uriResourceEntitySet = (UriResourceEntitySet) sourceUriResource;
        startEntitySet = uriResourceEntitySet.getEntitySet();
        responseEntitySet = Util.getNavigationTargetEntitySet(uriInfo);
        EdmEntityType targetEntityType = edmNavigationProperty.getType();
        List<UriParameter> keyParameters = uriResourceEntitySet.getKeyPredicates();

        Entity sourceEntity = storage.readEntityData(startEntitySet, keyParameters);
        if (sourceEntity == null) {
            throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ROOT);
        }
        responseEntityCollection = storage.getRelatedEntityCollection(sourceEntity, targetEntityType);
    }

    ContextURL contextUrl = ContextURL.with().entitySet(responseEntitySet).build();

    final String id = request.getRawBaseUri() + "/" + responseEntitySet.getName();
    EntityCollectionSerializerOptions options = EntityCollectionSerializerOptions.with().id(id).contextURL(contextUrl).build();
    EdmEntityType edmEntityType = responseEntitySet.getEntityType();

    ODataSerializer serializer = odata.createSerializer(responseFormat);
    SerializerResult serializerResult = serializer.entityCollection(serviceMetaData, edmEntityType, responseEntityCollection, options);
    InputStream inputStream = serializerResult.getContent();

    response.setContent(inputStream);
    response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
    response.setStatusCode(HttpStatusCode.OK.getStatusCode());

}


}

最佳答案

部门(x)是UriResourceEntitySet。您可能得到了它的EdmEntitySet。假设它存储在prevEdmEntitySet变量中。

员工(x)是UriResourceNavigation。您应该使用其名称,并将其与上述变量一起使用:

String propName = uriResourceNavigation.getProperty().getName();
EdmEntitySet currEdmEntitySet = (EdmEntitySet) startEdmEntitySet.getRelatedBindingTarget(propName);


然后,您应该从prevEdmEntitySet=currEdmEntitySet开始下一次迭代,并对所有UriResourceNavigation重复一次(在您的示例中仅重复一次)。

10-06 05:34