unirest中的杰克逊例外:序列化不可能。找不到ObjectMapper实现

时间:2020-12-04 21:01:20
ObjectMapper mapper = new ObjectMapper();
ObjectNode object = mapper.readValue("{\"myjson\":\"string\"}", ObjectNode.class);
HttpResponse<JsonNode> postResponse = Unirest.post("")
    .header("accept", "application/json")
    .body(object)
    .asJson();

Causes the runtime exception in the title on the call to body. I'm not sure what to do here, I'm attempting to wrap a json string in a JsonNode object which I'd assume is normal procedure when sending via unirest interface (in the data body).

在调用body时标题中导致运行时异常。我不知道该怎么做,我正在尝试将json字符串包装在JsonNode对象中,我认为这是通过unirest接口(在数据体中)发送时的正常过程。

2 个解决方案

#1


1  

I don't think you can do it like that. You can't pass ObjectNode into body method. You might implement custom ObjectMapper for that type. But I don't think that it's what you want.

我认为你不能那样做。您无法将ObjectNode传递给body方法。您可以为该类型实现自定义ObjectMapper。但我认为这不是你想要的。

Perhaps you can do something like this:

也许你可以这样做:

.header("Content-Type", "application/json")
.body(mapper.writeValueAsString(object))

#2


0  

Looking at the body implementation of HttpRequestWithBody:

看看HttpRequestWithBody的body实现:

public RequestBodyEntity body(Object body) {
    ObjectMapper objectMapper = (ObjectMapper) Options.getOption(Option.OBJECT_MAPPER);

    if (objectMapper == null) {
        throw new RuntimeException("Serialization Impossible. Can't find an ObjectMapper implementation.");
    }

    return body(objectMapper.writeValue(body));
}

It seems that passing an objet is ok if an ObjectMapper is available

如果ObjectMapper可用,似乎传递objet是可以的

#1


1  

I don't think you can do it like that. You can't pass ObjectNode into body method. You might implement custom ObjectMapper for that type. But I don't think that it's what you want.

我认为你不能那样做。您无法将ObjectNode传递给body方法。您可以为该类型实现自定义ObjectMapper。但我认为这不是你想要的。

Perhaps you can do something like this:

也许你可以这样做:

.header("Content-Type", "application/json")
.body(mapper.writeValueAsString(object))

#2


0  

Looking at the body implementation of HttpRequestWithBody:

看看HttpRequestWithBody的body实现:

public RequestBodyEntity body(Object body) {
    ObjectMapper objectMapper = (ObjectMapper) Options.getOption(Option.OBJECT_MAPPER);

    if (objectMapper == null) {
        throw new RuntimeException("Serialization Impossible. Can't find an ObjectMapper implementation.");
    }

    return body(objectMapper.writeValue(body));
}

It seems that passing an objet is ok if an ObjectMapper is available

如果ObjectMapper可用,似乎传递objet是可以的