如何使用Apache Wink RestClient将JSON数据发布到Web服务?
问题描述:
我试图做从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” 。
任何意见或建议,非常感谢!
罗布
答
你的代码看起来只是我希望后用一个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
您的代码看起来只是我预计'.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