【问题标题】:Specifying entity type using Apache Olingo使用 Apache Olingo 指定实体类型
【发布时间】:2018-12-21 06:34:55
【问题描述】:

我在我的项目中使用 Apache Olingo v4 java 库,它运行良好。谢谢!

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

https://docs.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]

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

作为参考,以下请求正文使用邮递员(普通的旧 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"
} 

提前感谢您花时间帮助我。

【问题讨论】:

  • 也许您想访问stackoverflow.com/editing-help 并检查您是否可以对代码格式进行任何改进。以目前的形式很难阅读(和回答)它。
  • 我修复了格式;请点击edit链接查看我做了什么来了解它是如何完成的

标签: java odata dynamics-crm crm olingo


【解决方案1】:

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

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 有一个菜单项可以做到这一点。或者您可以手动添加“;”并且大部分时间都在打字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-10
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多