Jersey: MessageBodyWriter not found for media type=application/json, type=class org.codehaus.jackson.node.ObjectNode?(泽西岛:找不到媒体类型 = 应用程序/json、类型 = 类 org.codehaus.jackson.node.ObjectNode 的 MessageBodyWriter?)
问题描述
我正在使用 Jersey 2.8 Client
将数据发布到 RESTful 端点.代码看起来像
I am using Jersey 2.8 Client
to post data to RESTful endpoint. The code looks like
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);
}
当我尝试运行测试时,我看到错误为
When I try to run the test, I see error as
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 没问题,那么你需要做以下 3 件事.
If you ok using Jackson 1.x, then you need to the following 3 things.
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
注册提供程序:
3. Register providers with 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);
这篇关于泽西岛:找不到媒体类型 = 应用程序/json、类型 = 类 org.codehaus.jackson.node.ObjectNode 的 MessageBodyWriter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:泽西岛:找不到媒体类型 = 应用程序/json、类型 = 类 org.codehaus.jackson.node.ObjectNode 的 MessageBodyWriter?
基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 降序排序:Java Map 2022-01-01