繁体   English   中英

如何在 Apache Olingo V4 Java API 中使用字符串枚举

[英]How to use String enumerations with Apache Olingo V4 Java API

我只是无法弄清楚如何使用CsdlEnumType数据创建期间阿帕奇Olingo V4的Java API。

这是我到目前为止用尽可能少的代码所做的:

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添加为字符串。 知道如何解决这个问题吗?

不知道你还有没有遇到这个问题。 但也许我的回答对某人有用。 根据OASIS OData 文档

枚举类型被命名为基本类型,其值被命名为具有底层整数值的常量。

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

而在代码中你可以使用valueOfString(name, null, null, null, null, null, Long)的方法EdmEnumType

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM