简体   繁体   English

Jersey:找不到媒体类型= application / json的MessageBodyWriter,类型= class org.codehaus.jackson.node.ObjectNode?

[英]Jersey: MessageBodyWriter not found for media type=application/json, type=class org.codehaus.jackson.node.ObjectNode?

I am using Jersey 2.8 Client to post data to RESTful endpoint. 我使用Jersey 2.8 Client将数据发布到RESTful端点。 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() looks like 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

What am I missing here? 我在这里错过了什么?

Thanks 谢谢

If you ok using Jackson 1.x, then you need to the following 3 things. 如果你可以使用Jackson 1.x,那么你需要做以下3件事。

1. Add Jersey Jackson to your pom.xml: 1.将Jersey Jackson添加到你的pom.xml:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.8</version>
</dependency>

2. Create a ContextResolver : 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. Register providers with ClientBuilder : 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);

I added a comment above stating that the addition of X had worked. 我在上面添加了一条评论,说明X的添加有效。

However, adding the following maven dependency to the pom.xml works as well, and seems like a more standard fix. 但是,将以下maven依赖项添加到pom.xml也可以,并且看起来像是一个更标准的修复。

     <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>

Note: The org.glassfish.jersey.archetypes/jersey-quickstart-grizzly maven archetype adds the dependency above by default, but commented out with the comment "uncomment this to get JSON support" . 注意:默认情况下, org.glassfish.jersey.archetypes / jersey-quickstart-grizzly maven原型添加了上面的依赖项,但注释掉了“取消注释以获得JSON支持”

I had the same issue when trying to get the response from an Apache server running PHP. 尝试从运行PHP的Apache服务器获取响应时,我遇到了同样的问题。 My response was good from the server, but Spring was complaining with the MessageBodyWriter not found for type application/json. 我对服务器的反应很好,但Spring抱怨找不到类型为application / json的MessageBodyWriter。 I added the Genson dependency to my pom.xml and it fixed it! 我将Genson依赖项添加到我的pom.xml中并修复了它!

    <dependency>
        <groupId>com.owlike</groupId>
        <artifactId>genson</artifactId>
        <version>0.99</version>
    </dependency>

Documentation can be found at: https://code.google.com/p/genson/ 有关文档, 访问: https//code.google.com/p/genson/

try to write the ObjectNode as a String : 尝试将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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 com.fasterxml.jackson.databind.node.ObjectNode无法转换为org.codehaus.jackson.node.ObjectNode - com.fasterxml.jackson.databind.node.ObjectNode cannot be converted to org.codehaus.jackson.node.ObjectNode 泽西岛+灰熊HTTP +杰克逊(未针对媒体类型= application / xml找到MessageBodyWriter) - Jersey + Grizzly HTTP + Jackson (MessageBodyWriter not found for media type=application/xml) 找不到针对媒体类型= application / json的杰克逊网络服务错误MessageBodyWriter - jackson web service error MessageBodyWriter not found for media type=application/json 泽西与Grizzly:找不到媒体类型= application / json的MessageBodyWriter - Jersey with Grizzly: Getting MessageBodyWriter not found for media type=application/json 泽西(Jersey)REST错误,找不到媒体类型= application / json的MessageBodyWriter - Jersey REST error, MessageBodyWriter not found for media type=application/json 找不到针对媒体类型=应用程序/八位字节流,类型=类org.glassfish.jersey.media.multipart.MultiPart的MessageBodyWriter - MessageBodyWriter not found for media type=application/octet-stream, type=class org.glassfish.jersey.media.multipart.MultiPart 找不到使用GF4和Jackson的media type = application / json的MessageBodyWriter - MessageBodyWriter not found for media type=application/json with GF4 and Jackson 未找到 Media type=application/json 的 MessageBodyWriter - MessageBodyWriter not found for media type=application/json 找不到媒体类型= application / json的MessageBodyWriter - Getting MessageBodyWriter not found for media type=application/json 找不到媒体类型application / json的MessageBodyWriter - MessageBodyWriter not found for media type application/json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM