【问题标题】:How to create an entity in Apache Olingo?如何在 Apache Olingo 中创建实体?
【发布时间】:2017-01-23 14:54:31
【问题描述】:

OData官方教程展示了如何创建实体:http://www.odata.org/getting-started/basic-tutorial/#create

我想使用 Templier 关于使用 Olingo (https://templth.wordpress.com/2014/12/05/manipulating-data-of-odata-v4-services-with-olingo/) 操作 OData v4 服务数据的教程在 Apache Olingo 中重现这一点

很遗憾,本教程已过时,我无法创建成功的 POST 请求。尝试执行我的代码会产生HTTP/1.1 500 Internal Server Error。

这是我目前的代码:

String serviceRoot = "http://services.odata.org/V4/TripPinService/";    
String namespace = "Microsoft.OData.SampleService.Models.TripPin";

URI personURI = client.newURIBuilder(serviceRoot)
            .appendEntitySetSegment("People").build();      
FullQualifiedName personFqn = new FullQualifiedName(namespace, "Person");

ClientEntity personEntity = client.getObjectFactory().newEntity(personFqn);     
personEntity.getProperties().add(
            client.getObjectFactory().newPrimitiveProperty(
                    "username",
                    client.getObjectFactory().newPrimitiveValueBuilder()
                            .buildString("MaryGilbert")));
//same procedure for "FirstName" and "LastName", which are obligatory fields

ODataEntityCreateRequest<ClientEntity> req = client.getCUDRequestFactory().getEntityCreateRequest(personURI,personEntity);
ODataEntityCreateResponse<ClientEntity> res = req.execute();

我做错了什么?我该怎么做才能进一步识别/调试问题?

非常感谢。

【问题讨论】:

    标签: java apache post odata olingo


    【解决方案1】:

    我认为属性名称区分大小写,因此请尝试使用 UserName 而不是用户名。此外,您可以使用 Fiddler 来调试您的问题。 这是您的用例的错误:

    HTTP/1.1 500 Internal Server Error - {"error":{"code":"InternalServerError","message":"The property 'UserName[Nullable=False]' of type 'Edm.String' has a null value
    

    【讨论】:

    • 天哪,这实际上是问题所在。将username 调整为UserName 后,我现在收到Status Code 201。非常感谢你。进一步感谢 Fiddler 的提示。这个工具看起来很有趣。
    猜你喜欢
    • 2017-06-10
    • 1970-01-01
    • 2018-12-21
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多