Java 泽西岛:找不到 Media type=application/json,type=class org.codehaus.jackson.node.ObjectNode 的 MessageBodyWriter?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23601842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:15:49  来源:igfitidea点击:

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

javajsonspringrestjersey

提问by daydreamer

I am using Jersey 2.8 Clientto post data to RESTful endpoint. The code looks like

我正在使用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()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.Hymanson.node.ObjectNode, genericType=class org.codehaus.Hymanson.node.ObjectNode

What am I missing here?

我在这里缺少什么?

Thanks

谢谢

回答by Alden

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

如果您可以使用 Hymanson 1.x,那么您需要做以下 3 件事。

1. Add Jersey Hymanson to your pom.xml:

1. 将 Jersey Hymanson 添加到您的 pom.xml 中:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-Hymanson</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(HymansonFeature.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);

回答by wingman88

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!

尝试从运行 PHP 的 Apache 服务器获取响应时遇到了同样的问题。我的服务器响应很好,但是 Spring 抱怨找不到类型 application/json 的 MessageBodyWriter。我将 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/

回答by herau

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

回答by Farskeptic

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-grizzlymaven 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-grizzlymaven 原型默认添加了上面的依赖项,但注释掉了“取消注释以获取 JSON 支持”