1

我正在R2DBC使用 spring bootWebFluxPostgresql. 我已经成功配置了在我的本地机器上运行的 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预期的那样。我认为这可能与数据库配置有关。但我找不到这个的根课程..

4

1 回答 1

1

Postman 不支持返回流。

有一个开放的功能请求,但它自 2018 年以来一直存在。

https://github.com/postmanlabs/postman-app-support/issues/5040

您可以使用 curl 并使用-N标志禁用缓冲来流式传输响应。

curl -N <your_url>

如果你不想使用 curl,只是为了测试,那么你可以collectList返回 aMono<List<T>>给客户端,并使用 postman。

于 2020-05-09T21:10:28.370 回答