2013-12-14 71 views
20

如何向澤西島2客戶提交空身材的發佈請求?與澤西島2客戶發佈空身

final MyClass result = ClientBuilder.newClient() 
    .target("http://localhost:8080") 
    .path("path") 
    .queryParam("key", "value") 
    .request(APPLICATION_JSON) 
    .post(What to fill in here if the body should be left empty??, MyClass.class); 

更新:這個作品:

final MyClass result = ClientBuilder 
    .newBuilder().register(JacksonFeature).build() 
    .target("http://localhost:8080") 
    .path("path") 
    .queryParam("key", "value") 
    .request(APPLICATION_JSON) 
    .post(null, MyClass.class); 
+0

我遲到了,但FWIW您的更新是回答我的具體問題 - 特別是我不想要一個Content-Type頭:) –

回答

18

我不能在文檔的任何地方發現這一點,但我相信你可以使用null得到一個空體:

final MyClass result = ClientBuilder.newClient() 
    .target("http://localhost:8080") 
    .path("path") 
    .queryParam("key", "value") 
    .request(APPLICATION_JSON) 
    .post(Entity.json(null), MyClass.class) 
+0

我試過了,但有一個'MessageBodyProviderNotFoundException:MessageBodyWriter未找到媒體類型= application/json,...'? – Stine

+0

那麼這是一個完全不同的問題......你應該註冊默認的JacksonFeature,或者編寫自己的MessageBodyWriter/Reader – Alden

+0

啊我看到你在上面寫道,對不起。所以'null'是正確的答案,對吧? – Alden

4

我不知道版本是否改變它。但是,下面不工作:

builder.put(Entity.json(null) );

其中,以下工作正常:

builder.put(Entity.json("") );

5

我發現,這個工作對我來說:

Response r = client 
    .target(url) 
    .path(path) 
    .queryParam(name, value) 
    .request() 
    .put(Entity.json("")); 

通行證一個空字符串,不是空值。

+0

非常感謝,它也適用於我。 –

2

只需發佈一個空的txt。

.post(Entity.text(""));