我正在使用Sping Boot 在Java中创建REST API。我遇到了一个我不理解的问题:当我通过vscode运行我的代码时,一切都很好。但是当我试图运行编译后的.jar文件时,我得到以下错误消息:
<message>org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Failed to determine a suitable driver class</message>
<frame>
<class>org.springframework.beans.factory.support.ConstructorResolver</class>
<method>createArgumentArray</method>
<line>798</line>
</frame>
它是一个PostgreSQL数据库。
我猜我的 * pom.xml * 文件可能有问题?
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.languagehub</groupId>
<artifactId>languagehubpublicapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>languagehubpublicapi</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<scope>compile</scope>
</plugin>
</plugins>
</build>
</project>
下面是我的 application.properties 文件:
spring.datasource.url = jdbc:postgresql://localhost:5432/languagehub
spring.datasource.username = [USER]
spring.datasource.password = [PASSWORD]
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.datasource.driver-class-name=org.postgresql.Driver
server.port = 8080
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
谢谢你的阅读!如果你有任何想法,我会非常感谢你的帮助!我祝大家有一个美好的一天!
**编辑:**我修改了 pom.xml 文件中的 postgresql 依赖项部分。现在我有以下内容:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
但我还是得到同样的错误...
2条答案
按热度按时间tvmytwxo1#
你的问题就在这里:
当你说runtime时,你是在告诉maven jar将在运行时提供(就像在deployment中一样),它不应该在捆绑中包含它。所以当你创建你的 Boot jar时,这个依赖项不会被包含。删除
scope
或者只是手动将它添加到CLI(不建议)。默认范围是compiled
VS代码在这里为您架起了差距,当您运行时,您只是没有意识到,它将它放在类路径上。
qeeaahzv2#
你试过从postgresql依赖中删除
<scope>runtime</scope>
吗?