有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用Apache Wink RestClient将JSON数据发布到web服务?

我试图通过发布来自Java的JSON数据来测试JAX-RS

我使用的是ApacheWink1.0和ApacheWinkRestClient。这个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");

。。。但我对发布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的编写器”

非常感谢您的任何想法或建议

罗布


共 (1) 个答案

  1. # 1 楼答案

    你的代码看起来非常正确,只是我希望帖子是用字符串实体完成的。因此,您可能需要更改:

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