Java Jackson无法访问com.fasterxml.Jackson.core.ObjectCodec
这是一个后续问题。我似乎无法使用以下代码访问jackson库:Java Jackson无法访问com.fasterxml.Jackson.core.ObjectCodec,java,json,maven,intellij-idea,jackson,Java,Json,Maven,Intellij Idea,Jackson,这是一个后续问题。我似乎无法使用以下代码访问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 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);
}
}
import com.fasterxml.jackson.databind.DeserializationFeature;
导入com.fasterxml.jackson.databind.ObjectMapper;
导入com.fasterxml.jackson.databind.ObjectReader;
导入com.fasterxml.jackson.databind.ObjectWriter;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
公共类服务器配置{
公共字符串信息=null;
公共字符串idlURL=null;
公共字符串idlContents=null;
public List servers=new ArrayList();
公开最终无效清除(){
info=null;
idlURL=null;
idlContents=null;
如果(服务器!=null)
服务器。清除();
}
私有最终静态ObjectReader jsonReader;
私有最终静态ObjectWriter jsonWriter;
静止的{
ObjectMapper mapper=新的ObjectMapper();
mapper.configure(反序列化功能.ACCEPT_SINGLE_VALUE_AS_ARRAY,true);//当我遇到这个问题时,我的类路径中有jackson注释和jackson数据绑定JAR,但不是jackson core
将jackson core添加到类路径为我解决了这个问题。我也遇到了同样的问题,结果是,我使用了“jackson core asl.jar”而不是“jackson core.jar”
因此,检查类路径中是否有这个jar文件。
->我也遇到了类似的问题,结果是IntelliJ问题
这帮助我解决了这个问题:
尝试File>Invalidate Caches>Invalidate并重新启动
如果没有帮助,请删除.idea
目录并从pom/gradle构建文件重新导入。还应再次检查所有Jackson依赖项是否使用相同的版本,如下所示:
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'
我以前有过jackson core:2.11.0
,这就是问题所在。我也遇到过这样的错误:无法访问com.fasterxml.jackson.core.TreeNode
我的maven POM的第一个版本包含:
jackson注释
jackson数据绑定
在我想为单元测试添加jackson-core
之前,它工作得很好。我添加了test
范围,出现了错误。经过一些调查,我发现为了使它工作,我必须删除test
范围,否则会出现此错误。如果有人有解释,我会感兴趣:) 在我的例子中,原因是我在类路径中有两个不同版本的Jackson库。如果使用Maven,还要确保使用正确的
值。这会告诉Maven何时以及在某些情况下如何解决依赖关系。可用值有编译,提供的,运行时<代码>测试
、系统
和导入
,您可以在此处阅读更多关于它们的操作: