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

匿名 (未验证) 提交于 2019-12-03 02:00:02

问题:

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

回答1:

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

1. Add Jersey Jackson to your pom.xml:

org.glassfish.jersey.mediajersey-media-json-jackson2.8

2. Create a ContextResolver:

@Provider public class ObjectMapperProvider implements ContextResolver {      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); 


回答2:

I added a comment above stating that the addition of X had worked.

However, adding the following maven dependency to the pom.xml works as well, and seems like a more standard fix.

     org.glassfish.jersey.mediajersey-media-moxy

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



回答3:

I had the same issue when trying to get the response from an Apache server running PHP. My response was good from the server, but Spring was complaining with the MessageBodyWriter not found for type application/json. I added the Genson dependency to my pom.xml and it fixed it!

    com.owlikegenson0.99

Documentation can be found at: https://code.google.com/p/genson/



回答4:

try to write the ObjectNode as a 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)); 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!