gpt4 book ai didi

java - 从 Java 7 到 Java 8 的 Jackson Object Mapper 版本化问题

转载 作者:搜寻专家 更新时间:2023-10-31 20:27:10 25 4
gpt4 key购买 nike

我正在尝试将 Java 版本从 1.7 升级到 1.8。除了在 Maven 插件中将目标版本升级到 1.8 之外,没有更改任何代码。

错误:无法访问 Versioned

来源:

private ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(data);

无法在 Internet 上的任何地方找到任何文档。我认为 1.8 应该是完全向后兼容的。

编辑:即使不更改 pom,如果我将本地 maven 设置为 Java 8,它仍然会崩溃。所以用 Java 8 编译有问题 :(

[INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.2.2:compile
[INFO] | +- org.codehaus.jackson:jackson-core-asl:jar:1.1.0:compile
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.2.2:compile
[INFO] +- com.fasterxml:jackson-xml-databind:jar:0.6.2:compile
[INFO] | +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.2:compile
[INFO] | +- org.codehaus.jackson:jackson-xc:jar:1.9.2:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.2.2:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.2.2:compile
[INFO] | +- org.codehaus.jackson:jackson-core-asl:jar:1.1.0:compile
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.2.2:compile
[INFO] +- com.fasterxml:jackson-xml-databind:jar:0.6.2:compile
[INFO] | +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.2:compile
[INFO] | +- org.codehaus.jackson:jackson-xc:jar:1.9.2:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.2.2:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.2.2:compile
[INFO] | +- org.codehaus.jackson:jackson-core-asl:jar:1.1.0:compile
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.2.2:compile
[INFO] +- com.fasterxml:jackson-xml-databind:jar:0.6.2:compile
[INFO] | +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.2:compile
[INFO] | +- org.codehaus.jackson:jackson-xc:jar:1.9.2:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.2.2:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.2.2:compile
[INFO] | +- org.codehaus.jackson:jackson-core-asl:jar:1.1.0:compile
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.2.2:compile
[INFO] +- com.fasterxml:jackson-xml-databind:jar:0.6.2:compile
[INFO] | +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.2:compile
[INFO] | +- org.codehaus.jackson:jackson-xc:jar:1.9.2:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.2.2:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.2.2:compile
[INFO] | +- org.codehaus.jackson:jackson-core-asl:jar:1.1.0:compile
[INFO] | +- org.codehaus.jackson:jackson-jaxrs:jar:1.9.11:compile
[INFO] | +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.11:compile
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.2.2:compile
[INFO] +- com.fasterxml:jackson-xml-databind:jar:0.6.2:compile
[INFO] | +- org.codehaus.jackson:jackson-xc:jar:1.9.2:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.2.2:compile
[INFO] +- org.codehaus.jackson:jackson-core-asl:jar:1.9.13:compile
[INFO] | | +- org.codehaus.jackson:jackson-jaxrs:jar:1.9.11:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.6.0-rc2:compile
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.2.2:compile
[INFO] +- com.fasterxml:jackson-xml-databind:jar:0.6.2:compile
[INFO] | +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.2:compile
[INFO] | +- org.codehaus.jackson:jackson-xc:jar:1.9.2:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.2.2:compile

-X输出:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project my-module: Compilation failure
[ERROR] /Users/xxxxxx/MyApp.java:[52,15] error: cannot access Versioned
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project myntra-orch-lms: Compilation failure
/Users/xxxxxx/MyApp.java:[52,15] error: cannot access Versioned

at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:862)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:286)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:197)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.CompilationFailureException: Compilation failure
/Users/xxxxxx/MyApp.java:[52,15] error: cannot access Versioned

at org.apache.maven.plugin.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:656)
at org.apache.maven.plugin.CompilerMojo.execute(CompilerMojo.java:128)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
... 20 more
[ERROR]
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

最佳答案

看起来您正在混合使用非常古老的 Jackson 版本。我怀疑在 Java 8 中,Maven 编译器插件使用的 .jar 文件的顺序可能与在 Java 7 中不同,因此可能找不到 Versioned 接口(interface)。这可能是一个问题,因为在 Java 8 中,java 编译器要求接口(interface)在编译实现时出现在类路径中 (JDK-8055048)。

Versioned 接口(interface)是在 1.6.0 中引入的,而您拥有 org.codehaus.jackson:jackson-core-asl 版本 1.1.0。这可能是问题的根本原因。

一个低风险的修复可能是将 org.codehaus.jackson:jackson-core-asl 版本升级到 1.9.2 以对应其他库,方法是在 .pom 文件中添加以下依赖项:

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.2</version>
</dependency>

作为长期解决方案,我建议考虑在所有项目中将 Jackson 版本升级到最新的 2.X(打包在 com.fastxml.jackson 中)并停止使用 1.X 版本(打包在 org.codehaus.jackson)中。

关于java - 从 Java 7 到 Java 8 的 Jackson Object Mapper 版本化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31555751/

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