简体   繁体   中英

Jackson cannot access com.fasterxml.jackson.core.ObjectCodec

This question is a follow up to this one . I can't seem to be able to access the jackson library in the following code:

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

}

eventhough the jar files are in the classpath(autocomplete shows the method declaration in Intellij).

What am I missing?

When I had this problem I had the jackson-annotations and jackson-databind jars in my classpath, but not jackson-core .

Adding jackson-core to the classpath solved it for me.

I was having a similar issue, it turned out to be an IntelliJ issue.

This helped me to resolve the issue:

Try File > Invalidate Caches > Invalidate and Restart .

If it doesn't help, delete .idea directory and reimport from pom/gradle build file.

Also double check that all your Jackson dependencies use the same version like below :

implementation 'com.fasterxml.jackson.core:jackson-core:2.10.1'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.10.1'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.10.1'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin:2.10.1'

I previously had jackson-core:2.11.0 and this was the issue.

I also encountered this kind of error: Cannot access com.fasterxml.jackson.core.TreeNode

My first version of the maven POM contained:

  • jackson-annotations
  • jackson-databind

It was working fine until I wanted to add jackson-core for unit tests. I added it with the test scope and the error appeared. After some investigation, I found out that to make it working, I had to remove the test scope, otherwise this error appears. If someone has an explanation I would be interested :)

I had also encountered the same problem, turns out, I had "jackson-core-asl.jar" instead of "jackson-core.jar"

So, check whether you have this jar-file in your classpath. -> https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM