我正在使用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
我在这里缺少什么?
由于
答案 0 :(得分:9)
如果你使用Jackson 1.x,那么你需要做以下三件事。
<强> 1。将Jersey Jackson添加到您的pom.xml:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.8</version>
</dependency>
<强> 2。创建ContextResolver
:
@Provider
public class ObjectMapperProvider implements ContextResolver<ObjectMapper> {
final ObjectMapper defaultObjectMapper;
public ObjectMapperProvider() {
defaultObjectMapper = getObjectMapper();
}
@Override
public ObjectMapper getContext(Class<?> type) {
return defaultObjectMapper;
}
public static 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);
}
}
第3。使用ClientBuilder
注册提供商:
final Client client = ClientBuilder.newBuilder()
.register(ObjectMapperProvider.class)
.register(JacksonFeature.class)
.build();
final WebTarget target = client.target(url).path("inventorySummary");
final ObjectNode payload = ObjectMapperProvider.getObjectMapper().createObjectNode();
payload.put("startDate", DateTime.now().toString());
payload.put("endDate", DateTime.now().plusDays(30).toString());
payload.put("networkId", 0);
final Response response = target.request(MediaType.APPLICATION_JSON)
.post(Entity.json(payload));
assertStatus(Response.Status.OK.getStatusCode(), response);
答案 1 :(得分:7)
我在上面添加了一条评论,说明X的添加有效。
但是,将以下maven依赖项添加到pom.xml也可以,并且看起来像是一个更标准的修复。
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
注意: org.glassfish.jersey.archetypes / jersey-quickstart-grizzly maven原型默认情况下会添加上面的依赖项,但是注释“取消注释以获取JSON支持“
答案 2 :(得分:5)
我在尝试从运行PHP的Apache服务器获取响应时遇到了同样的问题。我对服务器的反应很好,但Spring抱怨找不到类型为application / json的MessageBodyWriter。我将Genson依赖项添加到我的pom.xml中并修复了它!
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>0.99</version>
</dependency>
可在以下网址找到文档:https://code.google.com/p/genson/
答案 3 :(得分:1)
尝试将ObjectNode编写为String:
// your code
final ObjectNode payload = getObjectMapper().createObjectNode();
payload.put("startDate", DateTime.now().toString());
payload.put("endDate", DateTime.now().plusDays(30).toString());
payload.put("networkId", 0);
// the solution
String entity = getObjectMapper().writeValueAsString(payload);
final Response response = builder.accept(MediaType.APPLICATION_JSON).post(Entity.entity(entity, MediaType.APPLICATION_JSON));