java - 使用 Apache Olingo 指定实体类型

标签 java odata dynamics-crm crm olingo

我在我的项目中使用 Apache Olingo v4 java 库,它工作得很好。谢谢!

我在调用某个特定端点时遇到问题 -

https://learn.microsoft.com/en-us/dynamics365/customer-engagement/web-api/merge?view=dynamics-ce-odata-9

问题:无法在请求中指定实体类型。无法确定是否必须将其作为注释或其他内容传递。我尝试将其作为参数传递,但库随后将其类型附加为字符串参数。

预期请求正文包含:

    "@odata.type": "Microsoft.Dynamics.CRM.account"

实际请求正文包含:

    "@odata.type@odata.type": "String", 
    "@odata.type": "Microsoft.Dynamics.CRM.account"

上述请求导致 crm 内部出现异常,因为他们不需要此参数。

An error occurred while validating input parameters: Microsoft.OData.ODataException: Does not support untyped value in non-open type.
  at System.Web.OData.Formatter.Deserialization.DeserializationHelpers.ApplyProperty(ODataProperty property, IEdmStructuredTypeReference resourceType, Object resource, ODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext)
  at System.Web.OData.Formatter.Deserialization.ODataResourceDeserializer.ApplyStructuralProperties(Object resource, ODataResourceWrapper resourceWrapper, IEdmStructuredTypeReference structuredType, ODataDeserializerContext readContext)
  at Microsoft.Crm.Extensibility.OData.CrmODataEntityDeserializer.ApplyStructuralProperties(Object resource, ODataResourceWrapper resourceWrapper, IEdmStructuredTypeReference structuredType, ODataDeserializerContext readContext)
  at System.Web.OData.Formatter.Deserialization.ODataResourceDeserializer.ReadResource(ODataResourceWrapper resourceWrapper, IEdmStructuredTypeReference structuredType, ODataDeserializerContext readContext)
  at Microsoft.Crm.Extensibility.OData.CrmODataActionPayloadDeserializer.ReadEntry(ODataDeserializerContext readContext, ODataParameterReader reader, IEdmOperationParameter parameter)
  at Microsoft.Crm.Extensibility.OData.CrmODataActionPayloadDeserializer.Read(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext)
  at System.Web.OData.Formatter.ODataMediaTypeFormatter.ReadFromStream(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger) [HTTP/1.1 400 Bad Request]

我尝试向对象附加注释,但这些注释最终不会出现在请求正文中。

作为引用,以下请求正文使用 postman (普通的旧 http 客户端)工作:

{
    "Target": {
        "accountid": "b68c98c3-f339-e811-eeee-000d3a137a33",
        "@odata.type": "Microsoft.Dynamics.CRM.account"
    },
    "Subordinate": {
        "accountid": "f89a8c95-2353-e811-wwww-000d3a137896",
        "@odata.type": "Microsoft.Dynamics.CRM.account"
    },
    "UpdateContent": {
        "websiteurl": "testdata.com",
        "@odata.type": "Microsoft.Dynamics.CRM.account"
    },
    "PerformParentingChecks": "false"
} 

预先感谢您抽出时间来帮助我。

最佳答案

尝试使用“类型”系统时,使用 Olingo 客户端库可能会很麻烦。请在 groovy 中查看下面的代码片段,看看是否有帮助:

static AbstractODataInvokeRequest buildMergeRequest(ODataClient client, URI mergeUri,
        String targetLeadIdStr, String subLeadIdStr) {
    Map<String, ClientValue> params = [:]
    params.put("Target", buildEntityIdComplex(client, "leadid", targetLeadIdStr))
    params.put("Subordinate", buildEntityIdComplex(client, "leadid", subLeadIdStr))
    params.put("UpdateContent", buildEntityIdComplex(client, "leadid", "00000000-0000-0000-0000-000000000000"))
    params.put("PerformParentingChecks", client.getObjectFactory().newPrimitiveValueBuilder().buildBoolean(false))

    AbstractODataInvokeRequest funcReq =
            client.getInvokeRequestFactory().getActionInvokeRequest(mergeUri, ODataError.class, params)
                    .addCustomHeader("Authorization", "Bearer ${accessToken}")
    funcReq.setFormat(ContentType.JSON_NO_METADATA)
    return funcReq;
}

static ClientComplexValue buildEntityIdComplex(ODataClient client, String entityIdName, String entityIdValStr) {
    ClientComplexValue targetLeadId = client.getObjectFactory().
            newComplexValue("Microsoft.Dynamics.CRM.lead")
    targetLeadId.add(client.getObjectFactory().newPrimitiveProperty(entityIdName,
            client.getObjectFactory().newPrimitiveValueBuilder().buildGuid(buildKey(entityIdValStr))))
    targetLeadId.add(client.getObjectFactory().newPrimitiveProperty("@odata.type",
            client.getObjectFactory().newPrimitiveValueBuilder().buildString("#Microsoft.Dynamics.CRM.lead")))
    return targetLeadId
}

注意到:

  1. funcReq.setFormat(ContentType.JSON_NO_METADATA) 指示仅以 JSON 格式发送到服务器。
  2. 从 groovy 到 java,Intellij 有一个菜单项可以做到这一点。或者您可以手动添加“;”大部分时间都是打字。

关于java - 使用 Apache Olingo 指定实体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51315625/

相关文章:

java - spring mvc 不返回 json 内容 - 错误 406

java - 使用 JWT 配置 swagger

java - 为什么我的 View 没有改变?

java - 如何使字符串排序程序不区分大小写?

c# - 将 OAuth 与 OData 结合使用

python - 精确在线 API : deleting and updating items like BankEntryLines

javascript - 客户关系管理 2011 : Unwanted text in Case's Description field after populating with email body

plugins - 使用DynamicsCRM中的插件获取EntityReference的GUID

xml - 在哪里可以找到有关 Microsoft Dynamics CRM API 的 SOAP/XML 查询的信息

windows-phone-7 - 从 WP7 连接到 SQL Azure