【问题标题】:How to use String enumerations with Apache Olingo V4 Java API如何在 Apache Olingo V4 Java API 中使用字符串枚举
【发布时间】:2016-07-05 02:33:30
【问题描述】:

我只是不知道在数据创建过程中如何使用 Apaches Olingo V4 Java API 的CsdlEnumType

这是我迄今为止用尽可能少的代码所做的:

1) 在我的EdmODataProvider.java 类中,我创建了一个实体类型并将枚举实体的FQDN 添加到属性中。此外,我在模式提供程序类中实例化了CsdlEnumType。我想这行得通,因为如果我在setValue() 部分中只使用数字,我会得到预期的结果。 :

public CsdlEntityType getEntityType(FullQualifiedName entityTypeName) throws ODataException {
   CsdlEntityType entityType = new CsdlEntityType();
   List<CsdlProperty> properties = new ArrayList<CsdlProperty>();
   properties.add(new CsdlProperty().setName("Attributes").setType(new FullQualifiedName("Namespace", "Attributes")));
   entityType.setName("Langs").setProperties(properties);
   return entityType;
 }

 public List<CsdlSchema> getSchemas() throws ODataException {
    CsdlSchema schema = new CsdlSchema();
    schema.setNamespace("Namespace");
    List<CsdlEnumType> enumTypes = new ArrayList<CsdlEnumType>();
    enumTypes.add(
       new CsdlEnumType()
            .setName("LangAttributes")
            .setMembers(Arrays.asList(
                // if I use setValue("0") ... setValue("1") everything works fine
                new CsdlEnumMember().setName("DISPNAME").setValue("DISPNAME"),
                new CsdlEnumMember().setName("DESC").setValue("DESC")
        ))
    )
    // ... add entity type and set the other stuff
 }

2) 在我的数据提供者类中,我正在创建一个这样的实体:

Entity e = new Entity();
// again: it would work if I would use 0 instead of "DISPNAME" here
e.addProperty(new Property(null, "LangAttributes", ValueType.Enum, "DISPNAME"));

如果我尝试调用实体,我最终会收到错误消息:

<error xmlns="http://docs.oasis-open.org/odata/ns/metadata">
<code>400</code>
<message>The value 'DISPNAME' is not valid for property 'LangAttributes'.</message>
</error>

我的$metadata 包含:

<EnumType Name="Attribute" IsFlags="false" UnderlyingType="Edm.Int32">
   <Member Name="DISPNAME" Value="DISPNAME"/>
   <Member Name="DESC" Value="DESC"/>
</EnumType>
....
<EntityType Name="Attributes">
  <Property Name="LangAttributes" Type="Namespace.Attribute"/>
</EntityType>

我猜问题出在第 2 部分)我将属性 DISPNAME 添加为字符串。知道如何解决这个问题吗?

【问题讨论】:

    标签: java apache odata olingo odata-v4


    【解决方案1】:

    我不知道你是否仍然面临这个问题。但也许我的回答对某人有用。根据OASIS OData Documentation

    枚举类型是命名的基本类型,其值是具有基础整数值的命名常量。

    你不能在 Olingo 中创建一个枚举,它具有 String 底层类型。我的建议是从您的代码中跳过 .setValue("") 部分并依赖默认值(0、1、2 等)。不要害怕 - 您可以通过提供名称或值来对请求中的枚举值进行操作。所以只需依赖名称

    并且在代码中,您可以使用来自EdmEnumTypevalueOfString(name, null, null, null, null, null, Long) 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-14
      • 1970-01-01
      • 2019-06-21
      • 2014-03-21
      • 2014-07-23
      • 2017-03-22
      相关资源
      最近更新 更多