我正在R2DBC
使用 spring bootWebFlux
和Postgresql
. 我已经成功配置了在我的本地机器上运行的 PostgreSQL 数据库。我使用了以下依赖项和插件。
plugins {
id 'org.springframework.boot' version '2.3.0.BUILD-SNAPSHOT'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'eclipse'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
compile group: 'io.r2dbc', name: 'r2dbc-postgresql', version: '1.0.0.M7'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'io.projectreactor:reactor-test'
}
请找到我的代码
@SpringBootApplication
@EnableR2dbcRepositories
public class DemoServiceApplication {
public static void main(String[] args) {
SpringApplication.run(DemoServiceApplication.class, args);
}
}
@RestController
@RequiredArgsConstructor
class ResourceController {
final ResourceRepository resourceRepository;
@GetMapping("/method1")
public Flux<Resource> getResourcesMethod1(){
return resourceRepository.deleteAll().thenMany(
Flux.just(new Resource("name1", "description1", new Date()), new Resource("name2", "description2", new Date()))
.flatMap(resourceRepository::save))
.thenMany(
resourceRepository.findAll()
.flatMap(data -> {
return Flux.just(data);
}));
}
@GetMapping("/method2")
public Flux<Resource> getResourcesMethod2(){
return resourceRepository.findAll();
}
}
@Configuration
class DatabaseConfig extends AbstractR2dbcConfiguration {
@Bean
public ConnectionFactory connectionFactory() {
return new PostgresqlConnectionFactory(
PostgresqlConnectionConfiguration.builder()
.host("localhost")
.port(5432)
.username("postgres")
.password("password")
.database("mydatabase")
.build());
}
}
interface ResourceRepository extends ReactiveCrudRepository<Resource, Integer> {
}
@Data
class Resource {
@Id
Integer id;
final String name;
final String description;
final Date createdDate;
public Resource(String name, String description, Date createdDate) {
this.name = name;
this.description = description;
this.createdDate = createdDate;
}
}
从上面的代码(使用getResourcesMethod1
)我能够成功地删除数据并将数据插入到数据库表中。
但是,我无法从这两个休息端点检索到任何响应。我已经使用 Postman 进行测试,它无法检索任何东西......只是缓冲......
我在这里做错了什么?我可能会错过一个非常基本的东西。任何帮助将不胜感激。
编辑
如果我打印出通量,getResourcesMethod2
它将打印出FluxOnErrorResume
而不是FluxArray
预期的那样。我认为这可能与数据库配置有关。但我找不到这个的根课程..