SpringBoot+PostgreSQL项目构建报错:数据源配置与Gradle弃用问题
Spring Boot + PostgreSQL项目构建错误求助
环境信息
- 操作系统:Windows 11
- 开发工具:VS Code(编写Java代码)
- 构建与运行:通过CMD执行Gradle命令
项目背景
参考Spring Boot + PostgreSQL分步指南创建项目,用Spring Initializr生成初始结构,仅删除/修改了不需要的函数和变量。
遇到的问题
- 执行
./gradlew build时,出现Gradle版本兼容警告:
'Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0'
已尝试从Gradle 8.5降级到8.4,警告仍存在。
- 查看构建生成的HTML报告,发现核心数据源配置错误:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class
- 尝试在application.properties中添加
spring.datasource.driver-class-name=com.mysql.jdbc.Driver后,数据源错误消失,但出现Flyway Bean初始化失败:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flyway'...
- 执行
./gradlew build --warning-mode all后,收到Gradle测试框架依赖自动加载的弃用提示,不知道该怎么调整配置。
关键代码文件
build.gradle
plugins { id 'java' id 'org.springframework.boot' version '3.2.1' id 'io.spring.dependency-management' version '1.1.4' } group = 'com.example' version = '0.0.1-SNAPSHOT' java { sourceCompatibility = '17' } configurations { compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.flywaydb:flyway-core' compileOnly 'org.projectlombok:lombok' runtimeOnly 'org.postgresql:postgresql' annotationProcessor 'org.projectlombok:lombok' testImplementation 'junit:junit:4.12' testImplementation 'org.springframework.boot:spring-boot-starter-test' } tasks.named('test') { useJUnitPlatform() }
SmiTbackendProjectApplication.java
package com.example.SMITbackend; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @EnableJpaRepositories @SpringBootApplication public class SmiTbackendProjectApplication { public static void main(String[] args) { SpringApplication.run(SmiTbackendProjectApplication.class, args); } }
Contact.java
package com.example.SMITbackend.entity; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.Table; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @NoArgsConstructor @AllArgsConstructor @Data @Entity @Table(name = "contact") public class Contact { @Id private Integer id; private String realName; private String codeName; private String phoneNumber; }
ContactController.java
package com.example.SMITbackend.controller; import com.example.SMITbackend.entity.Contact; import com.example.SMITbackend.service.ContactService; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/contact") @RequiredArgsConstructor @Validated public class ContactController { private final ContactService contactService; @GetMapping("/") public ResponseEntity<List<Contact>> getAllContacts(){ return ResponseEntity.ok().body(contactService.getAllContacts()); } @PostMapping("/") public ResponseEntity<Contact> saveContact(@RequestBody Contact contact) { return ResponseEntity.ok().body(contactService.saveContact(contact)); } }
ContactRepository.java
package com.example.SMITbackend.repository; import org.springframework.data.jpa.repository.JpaRepository; import com.example.SMITbackend.entity.Contact; public interface ContactRepository extends JpaRepository<Contact, Integer>{ }
ContactService.java
package com.example.SMITbackend.service; import com.example.SMITbackend.entity.Contact; import com.example.SMITbackend.repository.ContactRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.util.List; import java.util.Optional; @Service @RequiredArgsConstructor @Slf4j public class ContactService { private final ContactRepository contactRepository; public List<Contact> getAllContacts(){ return contactRepository.findAll(); } public Contact saveContact (Contact contact){ Contact savedContact = contactRepository.save(contact); return contact; } }
application.properties
spring: application: name: springboot jackson: property-naming-strategy: SNAKE_CASE flyway: locations: classpath:/db/migration schemas: contacts baselineOnMigrate: true enabled: true datasource: driverClassName: org.postgresql.Driver url: jdbc:postgresql://localhost:5432/contacts?currentSchema=contacts username: postgres password: {my password}
内容的提问来源于stack exchange,提问作者Adren4linRu5h
相关产品推荐
相关产品推荐

