0

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):

        <!-- 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>

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:

        <!-- 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>

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?

wouterio
  • 137
  • 1
  • 10
  • Why don't you use the annotations from Jackson itself? [`com.fasterxml.jackson.annotation.JsonIgnore`](https://fasterxml.github.io/jackson-annotations/javadoc/2.7/com/fasterxml/jackson/annotation/JsonIgnore.html) – knittl Jun 26 '23 at 14:06
  • 2
    Why would upgrading from Java8 to Java17 need to move to Jakarta packages? Those are different things. So you are probably doing more then just upgrading to Java17 and are now mixing things. – M. Deinum Jun 26 '23 at 14:09
  • @M.Deinum Afaik the Javax package that was part of Java 8 is not included anymore in Java 17, so it's recommended to migrate from Javax to Jakarta. – wouterio Jun 26 '23 at 14:25
  • @knittl Using Jackson's `JsonCreator` and `JsonProperty` annotations instead of Jakarta's `JsonbCreator` and `JsonbProperty` annotations gives the error I mentioned in my post. And mixing annotations from Jackson and Jakarta may be possible, but feels a bit like I'm the wrong track. – wouterio Jun 26 '23 at 14:28
  • @wouterio you are wrong. The `jakarta` packages are the JavaEE packages, when upgrading the JDK you aren't changing those. And as you hare using Jackson that doesn't matter at all. So you must be upgrading more then just Java17. The only thing you would need are JAXB related classes for XML. – M. Deinum Jun 27 '23 at 05:20
  • @M.Deinum Ow yeah sorry of course, I also upgraded the maven dependencies to the most recent versions. Still the question remains. Should I undo the migration from Javax to Jakarta, or what else? – wouterio Jun 27 '23 at 21:00
  • So as stated you didn't only u pgrade java 17. Without knowing what you had this question will be impossible to answer. However as mentioned by others as well you are using jackson and you shouldn't change those annotations in the first place. – M. Deinum Jun 28 '23 at 06:00
  • @M.Deinum Thank you. Please check my updated post. – wouterio Jul 03 '23 at 09:40
  • Those aren't the dependencies I'm interested in. Which Jackson dependencies and classes you are using. – M. Deinum Jul 03 '23 at 09:46
  • I don't explicitly include Jackson dependencies, but `jersey-media-json-jackson` refers to `jackson-databind`, `jackson-module-jakarta-xmlbind-annotations`, `jackson-anotations`. I only use the `com.fasterxml.jackson.annotation` annotations `JsonCreator`, `JsonProperty`, `JsonAlias`, `JsonIgnore`. That counts for the old setup that's running fine, and the new setup. – wouterio Jul 03 '23 at 10:45
  • Still stuck on this... Any more ideas? – wouterio Jul 10 '23 at 09:33

1 Answers1

0

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:

    <!-- 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>
wouterio
  • 137
  • 1
  • 10