使用jOOQ与Vertx时出现java.lang.NoSuchMethodError错误求助
问题:jOOQ生成的DAO调用findAll()出现NoSuchMethodError异常
问题现象
使用jOOQ生成的DAO执行CRUD操作时,insert()方法可正常运行,但调用findAll()方法时抛出java.lang.NoSuchMethodError异常,异常调用栈如下:
调用代码
Connection conn = DriverManager.getConnection(url, userName, password); Configuration jooqConfiguration = new DefaultConfiguration().set(conn); vehilceDao = new VehilceDao(jooqConfiguration, Vertx.currentContext().owner()); vehilceDao.findAll() .onComplete(result -> { if (result.succeeded()) { ... } else { ... } });
pom.xml配置
<dependencies> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-web</artifactId> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-junit5</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit-jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit-jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-pg-client</artifactId> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-sql-client</artifactId> </dependency> <dependency> <groupId>org.jooq</groupId> <artifactId>jooq</artifactId> <version>3.16.4</version> </dependency> <dependency> <groupId>io.github.jklingsporn</groupId> <artifactId>vertx-jooq-classic-jdbc</artifactId> <version>6.5.5</version> </dependency> <dependency> <groupId>org.jooq</groupId> <artifactId>jooq-postgres-extensions</artifactId> <version>3.16.4</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.jooq</groupId> <artifactId>jooq-codegen-maven</artifactId> <version>3.16.4</version> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.6.0</version> <!-- Update to the latest version if needed --> </dependency> <dependency> <groupId>io.github.jklingsporn</groupId> <artifactId>vertx-jooq-generate</artifactId> <version>6.5.5</version> </dependency> </dependencies> <configuration> <jdbc> <driver>org.postgresql.Driver</driver> <url>jdbc:postgresql://localhost:5432/cars</url> <user>postgres</user> <password>postgres</password> </jdbc> <generator> <name>io.github.jklingsporn.vertx.jooq.generate.classic.ClassicJDBCVertxGenerator</name> <database> <name>org.jooq.meta.postgres.PostgresDatabase</name> <includes>.*</includes> <inputSchema>public</inputSchema> <unsignedTypes>false</unsignedTypes> <forcedTypes> <forcedType> <name>BOOLEAN</name> <types>(?i:SMALLINT)</types> </forcedType> </forcedTypes> </database> <target> <packageName>com.abc.generated</packageName> <directory>${project.basedir}/src/main/java</directory> </target> <generate> <interfaces>true</interfaces> <daos>true</daos> <fluentSetters>true</fluentSetters> </generate> <strategy> <name>io.github.jklingsporn.vertx.jooq.generate.VertxGeneratorStrategy</name> </strategy> </generator> </configuration> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>${maven-compiler-plugin.version}</version> <configuration> <source>17</source> <!-- Java version --> <target>17</target> <!-- Java version --> <release>17</release> </configuration> </plugin> <plugin> <artifactId>maven-shade-plugin</artifactId> <version>${maven-shade-plugin.version}</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Main-Class>${launcher.class}</Main-Class> <Main-Verticle>${main.verticle}</Main-Verticle> </manifestEntries> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> </transformers> <outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar </outputFile> </configuration> </execution> </executions> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>${maven-surefire-plugin.version}</version> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>${exec-maven-plugin.version}</version> <configuration> <mainClass>io.vertx.core.Launcher</mainClass> <arguments> <argument>run</argument> <argument>${main.verticle}</argument> </arguments> </configuration> </plugin> </plugins> </build>
错误原因
这个异常的核心是依赖版本不兼容:
vertx-jooq-classic-jdbc6.5.5版本依赖的jOOQ最低版本是3.17.x,但你使用的jOOQ是3.16.4,版本过低。insert()能正常运行是因为它调用的jOOQ方法在3.16.4中仍存在,但findAll()内部调用的jOOQ方法(比如异步查询相关的方法签名)在3.16.4中不存在,而vertx-jooq 6.5.5是基于高版本jOOQ编写的,导致运行时找不到对应方法。
内容的提问来源于stack exchange,提问作者shan
相关产品推荐
相关产品推荐

