简体   繁体   English

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

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

I just can't figure out how to use the CsdlEnumType of Apaches Olingo V4 Java API during the data creation.我只是无法弄清楚如何使用CsdlEnumType数据创建期间阿帕奇Olingo V4的Java API。

Here is what I've done so far with as less code as possible:这是我到目前为止用尽可能少的代码所做的:

1) In my EdmODataProvider.java class I've created an entity type and added the FQDN of the enum entity to the properties. 1) 在我的EdmODataProvider.java类中,我创建了一个实体类型并将枚举实体的FQDN添加到属性中。 Furthermore I've instantiated the CsdlEnumType in the schema provider class.此外,我已经在模式提供程序类中实例化了CsdlEnumType I guess this works, because if I'm using only numbers in the setValue() part I'm getting the expected results.我想这行得通,因为如果我只在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) In my data provider class I'm creating an entity like this: 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"));

If I'm trying to call the entity I'm finally receiving the error:如果我尝试调用实体,我最终会收到错误消息:

<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>

My $metadata contains:我的$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>

I guess the problem is in Part 2) where I'm adding the attribute DISPNAME as String.我想问题出在第 2 部分)中,我将属性DISPNAME添加为字符串。 Any idea how to fix this issue?知道如何解决这个问题吗?

I don't know if you still facing this issue.不知道你还有没有遇到这个问题。 But maybe my answer will be useful for someone.但也许我的回答对某人有用。 According to the OASIS OData Documentation :根据OASIS OData 文档

Enumeration types are named primitive types whose values are named constants with underlying integer values.枚举类型被命名为基本类型,其值被命名为具有底层整数值的常量。

You simple cannot create an enum in Olingo, which has String underlying type.你不能在 Olingo 中创建一个枚举,它具有 String 基础类型。 My suggestion is to skip .setValue("") part from your code and rely on a default values (0, 1, 2 etc.).我的建议是从您的代码中跳过.setValue("")部分并依赖默认值(0、1、2 等)。 Don't be afraid - you can operate on an enum value in your requests by providing either name or value.不要害怕 - 您可以通过提供名称或值来对请求中的枚举值进行操作。 So just rely on the name .所以只靠名字

And in the code you can use valueOfString(name, null, null, null, null, null, Long) method from EdmEnumType .而在代码中你可以使用valueOfString(name, null, null, null, null, null, Long)的方法EdmEnumType

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

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