Florent Nowak
Florent Nowak

Reputation: 51

Odata V4 Client: getEntityCreate method

I developed an ODataClient in Java in order to create new entities. I am encountering difficulties to create new entities. I took the initiative to see all messages sent by my client with Fiddler.

        ODataEntityCreateRequest<ClientEntity>  request= 
            client.getCUDRequestFactory()
            .getEntityCreateRequest(new URI("http://localhost:8888/"), clientEntity);

    request.addCustomHeader("Content-Type", "application/json;odata.metadata=minimal");
    request.setAccept("application/json;odata=minimalmetadata");

    ODataEntityCreateResponse<ClientEntity> response = request.execute();

below the first line of the body I obtained with Fiddler:

17b
{"@odata.type":"#ODataDemo.Product", ....}

I tested manually with Fiddler to create a new entity and the first line of message body should be:

 {"odata.type":"ODataDemo.Product", ....}

I would like to know if it possible to set the body of the request with Odata in order to delete "@" and "#".

Thanks,

Upvotes: 1

Views: 493

Answers (1)

Florent Nowak
Florent Nowak

Reputation: 51

I found an alternative solution to this problem. I do not use entirely OData libraries. I created methods to to the post Request.

public void insertData(String entityName, Entity entity)
{       
    try {
        ResWrap<Entity> resW = new ResWrap<Entity>(new URI(this.baseURI.concat("/").concat(entityName)), "full", entity);
        ClientEntity clientEntity = this.client.getBinder().getODataEntity(resW);
        //String message = getMessageRebuild(client.getWriter().writeEntity(clientEntity, ContentType.APPLICATION_JSON));

        InputStream is = client.getWriter().writeEntity(clientEntity, ContentType.APPLICATION_JSON);

        if(is != null)
        {
            System.out.println("POST: "+post(this.baseURI.concat("/").concat(entityName), is));
            //System.out.println("POST:"+post("http://localhost:8888/"+entityName, is));
        }
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ODataSerializerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public String post(String url,InputStream message) throws Exception{
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    //post.addHeader("Content-Type", "application/json;odata.metadata=minimal");
    //post.addHeader("Accept", "application/json;odata=verbose");
    post.addHeader("Content-Type", "application/json");
    post.addHeader("Accept", "application/json");
    HttpEntity entity = new ByteArrayEntity(IOUtils.toByteArray(message));
    post.setEntity(entity);
    HttpResponse response = client.execute(post);
    String result = EntityUtils.toString(response.getEntity());
    return result;
}

insertData take two parameters : entityName + Entity I generated. I use the librairie org.apache.http to send the http message to the OData Server.

Upvotes: 1

Related Questions