jackson 异常:序列化不可能。找不到 ObjectMapper 实现

问题描述 投票:0回答:4
ObjectMapper mapper = new ObjectMapper();
ObjectNode object = mapper.readValue("{\"myjson\":\"string\"}", ObjectNode.class);
HttpResponse<JsonNode> postResponse = Unirest.post("")
    .header("accept", "application/json")
    .body(object)
    .asJson();

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

json serialization jackson unirest objectmapper
4个回答
2
投票

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

也许你可以这样做:

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

2
投票

根据文档,您需要设置ObjectMapper,例如

Unirest.config().setObjectMapper(new JacksonObjectMapper());

如果您已经根据需要配置了对象映射器,请将其传递给构造函数:

new JacksonObjectMapper(yourObjectMapper);

JacksonGson 实现的对象映射器有一个单独的依赖项


0
投票

查看

body
HttpRequestWithBody
实现:

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

如果有

ObjectMapper
可用的话,传递一个物体似乎是可以的


0
投票

虽然您确实可以通过

ObjectMapper
方法在 Unirest 中设置自己的自定义
Unirest.config().setObjectMapper()
,但无法轻松扩展 Unirest 提供的现有实现(例如,
Gson
对象是私有且数据的)类型适配器必须在
GsonBuilder
)中指定。 为了解决这个问题,我写了一个包装器。

我正在使用 Groovy,它错误地序列化了

GString
类,因此这是下面代码中发生的唯一自定义。

static {
    Unirest.config().setObjectMapper(new UnirestObjectMapper(Unirest.config().objectMapper))
}

class UnirestObjectMapper implements ObjectMapper {
    private final ObjectMapper objectMapper

    UnirestObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper
    }

    @Override
    <T> T readValue(String value, Class<T> valueType) {
        return objectMapper.readValue(value, valueType)
    }

    @Override
    <T> T readValue(String value, GenericType<T> genericType) {
        return objectMapper.readValue(value, genericType)
    }

    @Override
    String writeValue(Object value) {
        return objectMapper.writeValue(normalize(value))
    }

    /**
     * A recursive method to dig through an Object and normalize the values inside it.
     */
    Object normalize(Object value) {
        if (value instanceof Collection) {
            return (value as Collection).collect { v ->
                return normalize(v)
            }
        } else if (value instanceof Map) {
            return (value as Map).collectEntries { k, v ->
                return [
                        normalize(k),
                        normalize(v)
                ]
            }
        } else if (value instanceof GString) {
            // If we don't cast to a String below, it serializes as a Map of the properties of
            // the GString object, which is not what we want.
            return value as String
        }

        return value
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.