2016-07-19 164 views
0

我开发了一个Java中的ODataClient以创建新的实体。我在创建新实体方面遇到困难。我主动查看了我的客户与Fiddler发送的所有消息。Odata V4客户端:getEntityCreate方法

 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(); 

所述主体的所述第一行下面我使用Fiddler获得:

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

我手动测试使用Fiddler来创建一个新的实体和消息体的第一行应是:

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

我想知道是否可以用Odata设置请求的主体,以删除“@”和“#”。

谢谢,

回答

0

我找到了这个问题的替代解决方案。我不完全使用OData库。我创建了方法来发送请求。

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取两个参数:entityName +我生成的实体。 我使用librairie org.apache.http将http消息发送到OData服务器。