我試圖做從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」 。
任何意見或建議,非常感謝!
羅布
您的代碼看起來只是我預計'.post'是用'String.class',沒有一點非常'JSONObject.class'。 – Perception 2012-03-02 17:26:52
謝謝,改爲'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
@Perception - 請發表您的評論作爲答案,我會標記它是正確的! – 2012-03-02 18:18:30