如何在Apache Olingo中创建实体?

时间:2017-01-23 14:54:31

标签: java apache post odata olingo

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

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

很遗憾,本教程已过时,我无法创建成功的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();

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

非常感谢。

1 个答案:

答案 0 :(得分:2)

我认为属性名称区分大小写,因此请尝试使用UserName而不是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