我正在使用olingo使用OData v4协议进行休息服务。当我被告知时,我得到了导航的堆栈:
Company.svc / Departaments(X)/雇员(X)/ BussinesTrips
在Olingo的教程中,有一个例子是2段导航。第一段是UriResourceEntitySet,第二段是UriResourceNavigation。 在该示例中,需要这两个(特别是entitySet)作为方法中的参数,该方法从存储获得相关的实体集合。
在我的例子中有Company.svc / UriResourceEntitySet / UriResourceNavigation / UriResourceNavigation,我可以从UriInfo参数中说出来。
我不知道该怎么做。我应该更改方法还是以某种方式强制阴谋段为EntitySet。
感谢intrest,我等待回电。
@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());
}
}
答案 0 :(得分:1)
Departaments(x)是UriResourceEntitySet
。你可能得到了EdmEntitySet
。我们假设它存储在prevEdmEntitySet
变量中。
员工(x)是UriResourceNavigation
。您应该使用其名称并将其与上述变量一起使用:
String propName = uriResourceNavigation.getProperty().getName();
EdmEntitySet currEdmEntitySet = (EdmEntitySet) startEdmEntitySet.getRelatedBindingTarget(propName);
然后你应该用prevEdmEntitySet=currEdmEntitySet
开始下一次迭代并重复所有UriResourceNavigation
s(在你的例子中再一次)。