gpt4 book ai didi

java - 使用 ObjectReader 相对于 ObjectMapper 有什么好处?

转载 作者:行者123 更新时间:2023-12-02 13:04:53 30 4
gpt4 key购买 nike

如何使用 ObjectReader(ObjectReader 的单个实例)重构以下代码

private MessageStructure parseSQSMessageToMessageStructure(final String queueMessage)
throws IOException, ObjectMapperException {

JsonNode messageAsJSON = objectMapper.readTree(queueMessage);
JsonNode messageAsJSONKey = messageAsJSON.get(MESSAGE_JSON_KEY);
return objectMapper.readValue(messageAsJSONKey.textValue(), MessageStructure.class);
}

最佳答案

正如@shmosel所说,您的问题与标题不符。

如果您希望将 ObjectMapper 实例替换为 ObjectReader,请替换以下行。

来自:

return objectMapper.readValue(messageAsJSONKey.textValue(), MessageStructure.class);

future :

return objectReader.readValue(messageAsJSONKey.textValue(), MessageStructure.class);

上面的代码片段假设 objectReaderObjectReader 类的实例。

下面是我从 ObjectReader 的 JavaDocs 获得的有关其性能的信息。

Uses "mutant factory" pattern so that instances are immutable (and thus fully thread-safe with no external synchronization); new instances are constructed for different configurations. Instances are initially constructed by {@link ObjectMapper} and can be reused, shared, cached; both because of thread-safety and because instances are relatively light-weight.

希望这有帮助!

关于java - 使用 ObjectReader 相对于 ObjectMapper 有什么好处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44176035/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com