2012-03-02 66 views
2

我試圖做從Java JSON數據的POST來測試JAX-RS。如何使用Apache Wink RestClient將JSON數據發佈到Web服務?

我使用Apache Wink 1.0和Apache Wink RestClient。該docs說這是你如何做一個帖子...

RestClient client = new RestClient(); 
Resource resource = client.resource("http://services.co"); 
String response = resource.contentType("text/plain").accept("text/plain").post(String.class, "foo"); 

...但我做什麼更改POST JSON數據?

我嘗試這樣做:

JSONObject json = new JSONObject(); 
json.put("abc", 123); 

RestClient client = new RestClient(); 
Resource resource = client.resource("http://services.co"); 
JSONObject response = resource.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(JSONObject.class, json); 

...但我在文章中,我得到這個錯誤的異常:「沒有作家型類net.sf.json.JSONObject和媒體類型application/JSON」 。

任何意見或建議,非常感謝!

羅布

+0

您的代碼看起來只是我預計'.post'是用'String.class',沒有一點非常'JSONObject.class'。 – Perception 2012-03-02 17:26:52

+0

謝謝,改爲'String response = resource.content ... post(String.class,json)',現在客戶端很開心。但是,我有一個新的服務器問題,我添加了一個新問題 - 請幫助! :) 謝謝! http://stackoverflow.com/questions/9538342/whats-wrong-with-my-simple-json-jax-rs-web-service – 2012-03-02 18:17:46

+0

@Perception - 請發表您的評論作爲答案,我會標記它是正確的! – 2012-03-02 18:18:30

回答

1

你的代碼看起來只是我希望後用一個string實體進行非常正確的。因此,你可能要改變:

JSONObject response = resource.contentType(MediaType.APPLICATION_JSON) 
      .accept(MediaType.APPLICATION_JSON).post(JSONObject.class, json); 

要:

String response = resource.contentType(MediaType.APPLICATION_JSON) 
      .accept(MediaType.APPLICATION_JSON).post(String.class, json); 
+0

當我嘗試這個時,我得到了「令牌上的語法錯誤...」 – 2016-07-18 15:37:36

相關問題