【发布时间】:2015-06-17 18:30:49
【问题描述】:
我有一个 POJO 类,其中一个元素是 JSONObject 类型:
public static class TestRequest {
org.json.JSONObject data;
}
我想使用 JerseyClient 通过 POST 调用发送它。
我试过这个:
Client client = new Client();
TestRequest request = new TestRequest();
Map<Long, String> data = Maps.newHashMap();
data.put(1L, "abc");
JSONObject object = new JSONObject(data);
request.setData(object);
ClientResponse response = client.resource("http://localhost:18000/test/url")
.accept(MediaType.APPLICATION_JSON_TYPE)
.type(MediaType.APPLICATION_JSON_TYPE)
.post(ClientResponse.class, request);
这失败并出现错误: 找不到类 org.json.JSONObject 的序列化程序
然后我添加了这个:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
client.resource("http://localhost:18000/test/url")
.accept(MediaType.APPLICATION_JSON_TYPE)
.type(MediaType.APPLICATION_JSON_TYPE)
.post(ClientResponse.class, mapper.writeValueAsBytes(request));
这不会失败,但是在实际发送的请求正文中,data 为空 json。
知道如何让它工作吗?
将它保留为 JsonObject 的原因是我将从一个服务中获取它,并将其传递给另一个服务而不实际使用它。所以我不想为data的内部建模。
【问题讨论】:
标签: java json rest jersey jersey-client