【发布时间】:2014-06-29 09:23:22
【问题描述】:
我正在使用Jersey 2.8 Client 将数据发布到 RESTful 端点。代码看起来像
final Client client = ClientBuilder.newClient();
final WebTarget target = client.target(url).path("inventorySummary");
final Invocation.Builder builder = target.request().header("Content-Type", MediaType.APPLICATION_JSON);
final ObjectNode payload = getObjectMapper().createObjectNode();
payload.put("startDate", DateTime.now().toString());
payload.put("endDate", DateTime.now().plusDays(30).toString());
payload.put("networkId", 0);
final Response response = builder.accept(MediaType.APPLICATION_JSON).post(Entity.entity(payload, MediaType.APPLICATION_JSON));
assertStatus(Response.Status.OK.getStatusCode(), response);
final JsonNode jsonReply = parseResponse(response);
getObjectMapper() 看起来像
public ObjectMapper getObjectMapper() {
return new ObjectMapper()
.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false /* force ISO8601 */)
.configure(SerializationConfig.Feature.WRITE_ENUMS_USING_TO_STRING, true)
.configure(DeserializationConfig.Feature.READ_ENUMS_USING_TO_STRING, true)
.setSerializationInclusion(JsonSerialize.Inclusion.ALWAYS);
}
当我尝试运行测试时,我看到错误为
MessageBodyWriter not found for media type=application/json, type=class org.codehaus.jackson.node.ObjectNode, genericType=class org.codehaus.jackson.node.ObjectNode
我在这里错过了什么?
谢谢
【问题讨论】:
-
您似乎使用的是 Jackson 1.x。您可以升级到 Jackson 2.x 并重试吗?
-
您添加了 genson.jar 吗?如果没有,看看这个答案:stackoverflow.com/questions/17503748/…
标签: java json spring rest jersey