Using annotations with Apache Olingo for OData v4

时间:2016-07-11 23:29:03

标签: olingo java-annotations

I see there is documentation that describes using annotations with Olingo for Odata v2: https://olingo.apache.org/doc/odata2/tutorials/AnnotationProcessorExtension.html

So for example, instead of manually creating a provider that details all the EDM metadata, I'd like to add annotations to my model and have a generic EDM provider to generate all the meta data. And similarly for the data provider. It would like something like this:

@EdmEntityType
@EdmEntitySet
public class Car {
  @EdmKey
  @EdmProperty
  private String id;
  @EdmProperty
  private String model;
  @EdmNavigationProperty
  private Manufacturer manufacturer;
}

Is there similar functionality for Olingo Odata4? I couldn't find any examples and searching didn't seen the annotations defined in their source code. As I recall this approach works with other frameworks, .Nets web API, SDL Odata, Olingo Odata2, etc.

Update: I ended up using SDL Odata instead which also supports Odata v4 and has notations. For an example look here: https://github.com/sdl/odata-example

Here is what a model looks like in Scala:

@EdmEntity(namespace = "SDL.OData.Example", key = Array("id"), containerName = "SDLExample")
@EdmEntitySet
case class Person (
  @(EdmProperty @field)(name="id",       nullable = false) var personId: String,
  @(EdmProperty @field)(name="firstName",nullable = false) var firstName: String,
  @(EdmProperty @field)(name="lastName", nullable = false) var lastName: String,
  @(EdmProperty @field)(name="age",      nullable = false) var age: Int
)

1 个答案:

答案 0 :(得分:3)

目前没有关于此主题的文档。我建议你看一下用于集成测试的Olingo TechSvc模块。在那里,您可以看到如何为EntitySet设置注释:https://github.com/apache/olingo-odata4/blob/2e24ffd1d3c343fdec45f8dbf0398783a0a86f3f/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/provider/ContainerProvider.java#L179

基本上,您可以使用org.apache.olingo.commons.api.edm.annotation包中的类,并将这些clases的实例添加到您想要注释的CsdlProvider元素中。