Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

java - 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. 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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you ok using Jackson 1.x, then you need to the following 3 things.

1. Add Jersey Jackson to your pom.xml:

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

2. Create a 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:

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...