Jackson 無法存取 com.fasterxml.jackson.core.ObjectCodec

2022-09-01 18:36:07

这个问题是这个问题的后续。我似乎无法在以下代码中访问 jackson 库:

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ServerConfiguration {
    public String info = null;
    public String idlURL = null;
    public String idlContents = null;
    public List<ServerInfo> servers = new ArrayList<>();

    public final void clear() {
        info = null;
        idlURL = null;
        idlContents = null;
        if (servers != null)
            servers.clear();
    }

    private final static ObjectReader jsonReader;
    private final static ObjectWriter jsonWriter;

    static {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); // <== Error:(52, 15) java: cannot access com.fasterxml.jackson.core.JsonGenerator class file for com.fasterxml.jackson.core.JsonGenerator not found
        //mapper.configure(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, true);
        jsonWriter = mapper.writer();
        jsonReader = mapper.reader(ServerConfiguration.class);
    }

    public static ServerConfiguration fromJson(String json) throws IOException {
        return jsonReader.<ServerConfiguration>readValue(json); // <== Error:(59, 26) java: cannot access com.fasterxml.jackson.core.JsonProcessingException class file for com.fasterxml.jackson.core.JsonProcessingException not found
    }

    public String toJson() throws IOException {
        return jsonWriter.writeValueAsString(this);
    }

}

即使 jar 文件位于类路径中(autocomplete 在 Intellij 中显示方法声明)。

我错过了什么?


答案 1

当我遇到这个问题时,我的类路径中有 jackson-annotationsjackson-databind jars,但没有 jackson-core

将 jackson-core 添加到类路径中对我来说解决了这个问题。


答案 2

我遇到了类似的问题,事实证明这是一个IntelliJ问题。

这帮助我解决了这个问题:

尝试。File > Invalidate Caches > Invalidate and Restart

如果它没有帮助,请删除目录并从pom / gradle构建文件中重新导入。.idea