迁移 Jackson 从 Java 8 到 17

huangapple go评论71阅读模式
英文:

Migrating Jackson from Java 8 to 17

问题

我正在尝试将我的项目从Java 8升级到17。据我所知,这本质上包括将Javax替换为Jakarta。为此,我使用了Intellij的Javax->Jakarta迁移工具。我的类使用com.fasterxml.jackson.annotation注解,包括JsonCreatorJsonPropertyJsonAliasJsonIgnore

然而,使用Java 17运行一些测试时出现以下错误:

Caused by: jakarta.json.bind.JsonbException: 无法创建类的实例:类io.wouter.bolendpoints.entities.BolRateLimit,找不到默认构造函数。

其中io.wouter.bolendpoints.entities.BolRateLimit是我尝试反序列化的类。

用Jakarta注解JsonbCreatorJsonbProperty替换JsonCreatorJsonProperty注解可以解决错误。但是我还需要替代JsonAliasJsonIgnore,如果可能的话,我宁愿仍然使用Jackson注解。

在这种情况下,应该采取什么正确的方式呢?我找到的文档非常有限。

编辑:
根据@M.Deinum的评论,我使用了这些旧的依赖项(并将所有Jakarta导入还原为Javax)让事情重新运行起来:

        <!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-runtime -->
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-client -->
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>2.35</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.inject/jersey-hk2 -->
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>2.35</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.connectors/jersey-apache-connector -->
        <dependency>
            <groupId>org.glassfish.jersey.connectors</groupId>
            <artifactId>jersey-apache-connector</artifactId>
            <version>2.35</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>2.35</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.security/oauth2-client -->
        <dependency>
            <groupId>org.glassfish.jersey.security</groupId>
            <artifactId>oauth2-client</artifactId>
            <version>2.35</version>
        </dependency>

然而,javax.xml.bind.jaxb-api依赖项在2018年后没有更新,所以我想我应该仍然切换到Jakarta吧?以下是我将使用的依赖项:

        <!-- https://mvnrepository.com/artifact/jakarta.xml.bind/jakarta.xml.bind-api -->
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>4.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-runtime -->
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>4.0.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-client -->
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>3.1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.inject/jersey-hk2 -->
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>3.1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.connectors/jersey-apache-connector -->
        <dependency>
            <groupId>org.glassfish.jersey.connectors</groupId>
            <artifactId>jersey-apache-connector</artifactId>
            <version>3.1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>3.1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.security/oauth2-client -->
        <dependency>
            <groupId>org.glassfish.jersey.security</groupId>
            <artifactId>oauth2-client</artifactId>
            <version>3.1.2</version>
        </dependency>

但是然后我再次遇到了旧的错误:
Caused by: jakarta.json.bind.JsonbException: 无法创建类的实例:类io.wouter.bolendpoints.entities.BolRateLimit,找不到默认构造函数。

有没有办法继续前进?

英文:

I'm trying to upgrade my project from Java 8 to 17. Afaik this inherently includes replacing Javax with Jakarta. For that I used the Intellij Javax->Jakarta migration tool. My classes use com.fasterxml.jackson.annotation annotations JsonCreator JsonProperty JsonAlias JsonIgnore.

Running some tests with Java 17 however gives the following error:

Caused by: jakarta.json.bind.JsonbException: Cannot create instance of a class: class io.wouter.bolendpoints.entities.BolRateLimit, No default constructor found.

with io.wouter.bolendpoints.entities.BolRateLimit a class that 'm trying to deserialize.

Replacing the JsonCreator and JsonProperty annotations with Jakarta annotations JsonbCreator JsonbProperty solves the error. But then I also need a replacement for JsonAlias and JsonIgnore. And if possible I rather stick with the Jackson annotations anyway.

What is the right way to go here? I can find very little documentation about this.

EDIT:
After @M.Deinum's comments I got things running again with these old dependencies (and reverting all Jakarta imports to Javax):

        &lt;!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;javax.xml.bind&lt;/groupId&gt;
            &lt;artifactId&gt;jaxb-api&lt;/artifactId&gt;
            &lt;version&gt;2.3.1&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;!-- https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-runtime --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.glassfish.jaxb&lt;/groupId&gt;
            &lt;artifactId&gt;jaxb-runtime&lt;/artifactId&gt;
            &lt;version&gt;2.3.2&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-client --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
            &lt;artifactId&gt;jersey-client&lt;/artifactId&gt;
            &lt;version&gt;2.35&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;!-- https://mvnrepository.com/artifact/org.glassfish.jersey.inject/jersey-hk2 --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.glassfish.jersey.inject&lt;/groupId&gt;
            &lt;artifactId&gt;jersey-hk2&lt;/artifactId&gt;
            &lt;version&gt;2.35&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;!-- https://mvnrepository.com/artifact/org.glassfish.jersey.connectors/jersey-apache-connector --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.glassfish.jersey.connectors&lt;/groupId&gt;
            &lt;artifactId&gt;jersey-apache-connector&lt;/artifactId&gt;
            &lt;version&gt;2.35&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.glassfish.jersey.media&lt;/groupId&gt;
            &lt;artifactId&gt;jersey-media-json-jackson&lt;/artifactId&gt;
            &lt;version&gt;2.35&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;!-- https://mvnrepository.com/artifact/org.glassfish.jersey.security/oauth2-client --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.glassfish.jersey.security&lt;/groupId&gt;
            &lt;artifactId&gt;oauth2-client&lt;/artifactId&gt;
            &lt;version&gt;2.35&lt;/version&gt;
        &lt;/dependency&gt;

However, the javax.xml.bind.jaxb-api dependency wasn't updated after 2018, so I guess I should still switch to Jakarta? These are the dependencies I'd use for that:

        &lt;!-- https://mvnrepository.com/artifact/jakarta.xml.bind/jakarta.xml.bind-api --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;jakarta.xml.bind&lt;/groupId&gt;
            &lt;artifactId&gt;jakarta.xml.bind-api&lt;/artifactId&gt;
            &lt;version&gt;4.0.0&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;!-- https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-runtime --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.glassfish.jaxb&lt;/groupId&gt;
            &lt;artifactId&gt;jaxb-runtime&lt;/artifactId&gt;
            &lt;version&gt;4.0.1&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-client --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
            &lt;artifactId&gt;jersey-client&lt;/artifactId&gt;
            &lt;version&gt;3.1.2&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;!-- https://mvnrepository.com/artifact/org.glassfish.jersey.inject/jersey-hk2 --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.glassfish.jersey.inject&lt;/groupId&gt;
            &lt;artifactId&gt;jersey-hk2&lt;/artifactId&gt;
            &lt;version&gt;3.1.2&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;!-- https://mvnrepository.com/artifact/org.glassfish.jersey.connectors/jersey-apache-connector --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.glassfish.jersey.connectors&lt;/groupId&gt;
            &lt;artifactId&gt;jersey-apache-connector&lt;/artifactId&gt;
            &lt;version&gt;3.1.2&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.glassfish.jersey.media&lt;/groupId&gt;
            &lt;artifactId&gt;jersey-media-json-jackson&lt;/artifactId&gt;
            &lt;version&gt;3.1.2&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;!-- https://mvnrepository.com/artifact/org.glassfish.jersey.security/oauth2-client --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.glassfish.jersey.security&lt;/groupId&gt;
            &lt;artifactId&gt;oauth2-client&lt;/artifactId&gt;
            &lt;version&gt;3.1.2&lt;/version&gt;
        &lt;/dependency&gt;

But then I get the old error again:
Caused by: jakarta.json.bind.JsonbException: Cannot create instance of a class: class io.wouter.bolendpoints.entities.BolRateLimit, No default constructor found.

Any ideas how to move on from here?

答案1

得分: 0

好的,以下是翻译好的部分:

所以,事实证明,jakarta.xml.bind-apijaxb-runtime 依赖项是多余的,而 oauth2-client 依赖项也不是必需的,但在某种程度上搞乱了一些东西。使用下面的依赖关系,现在似乎一切正常运行:

<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-client -->
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>3.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.inject/jersey-hk2 -->
<dependency>
    <groupId>org.glassfish.jersey.inject</groupId>
    <artifactId>jersey-hk2</artifactId>
    <version>3.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.connectors/jersey-apache-connector -->
<dependency>
    <groupId>org.glassfish.jersey.connectors</groupId>
    <artifactId>jersey-apache-connector</artifactId>
    <version>3.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson -->
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>3.1.2</version>
</dependency>
英文:

Ok, so it turned out that the jakarta.xml.bind-api and jaxb-runtime dependencies were superfluous, and the oauth2-client dependency wasn't needed either but screwed things up some way or another. With the dependencies underneath, it all seems to works now:

    &lt;!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-client --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
        &lt;artifactId&gt;jersey-client&lt;/artifactId&gt;
        &lt;version&gt;3.1.2&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;!-- https://mvnrepository.com/artifact/org.glassfish.jersey.inject/jersey-hk2 --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.glassfish.jersey.inject&lt;/groupId&gt;
        &lt;artifactId&gt;jersey-hk2&lt;/artifactId&gt;
        &lt;version&gt;3.1.2&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;!-- https://mvnrepository.com/artifact/org.glassfish.jersey.connectors/jersey-apache-connector --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.glassfish.jersey.connectors&lt;/groupId&gt;
        &lt;artifactId&gt;jersey-apache-connector&lt;/artifactId&gt;
        &lt;version&gt;3.1.2&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.glassfish.jersey.media&lt;/groupId&gt;
        &lt;artifactId&gt;jersey-media-json-jackson&lt;/artifactId&gt;
        &lt;version&gt;3.1.2&lt;/version&gt;
    &lt;/dependency&gt;

huangapple
  • 本文由 发表于 2023年6月26日 22:01:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定